When I am trying to use the Excel left() worksheet function in visual studio Excel-JS api, it does not work at all! this means there is no error message, there is no value and not a clue what is not working
I am talking about this statement:
onzin = ctx.workbook.functions.left(Acode.values.toString(), 7);
onzin.load("values","address");
According to the (incredibly incomplete) documentation from Microsoft https://learn.microsoft.com/en-us/javascript/api/excel/excel.worksheetcollection?view=office-js
you have to load the variable in the Excel Object model before you use it and after a synchronization of the context, the values are available. But in my 2nd promise I loaded the values and the address of "onzin" but when I want to assign the values to the ICcode Range, it does not do it and furthermore, when I put a breakpoint in it, it seams to be empty and no errors.
Here is my code:
Excel.run(function (ctx) {
//always use the Data sheet
var MyDataSheet = ctx.workbook.worksheets.getItem("Data");
var ConfigSheet = ctx.workbook.worksheets.getItem("Config");
var onzin;
MyDataSheet.activate();
var productStartRange = MyDataSheet.getUsedRange();
//define a range before the values can be loaded to the Excel Object model
var Acode = MyDataSheet.getRange("A3").load("values, address");
productStartRange.load("values, address, length");
// Run the queued-up commands, and return a promise to indicate task completion
return ctx.sync()
.then(function () {
var myBounds = GetBounds(productStartRange);
ConfigSheet.activate();
//put the column and row bounds in the config sheet
var ColBounds = ConfigSheet.getRange("B22");
var RowBounds = ConfigSheet.getRange("B21");
var NumProducts = ConfigSheet.getRange("B34");
var NumProperties = ConfigSheet.getRange("B27");
//ICcode.values = ctx.workbook.functions.left(Acode.values.toString(), 7);
onzin = ctx.workbook.functions.left(Acode.values.toString(), 7);
onzin.load("values","address");
//ICcode.values = Acode.values.toString().substring(0, 7);
//ICcode.values = onzin.values;
ColBounds.values = myBounds.LastCol;
RowBounds.values = myBounds.LastRow - 1;
//total number of products
NumProducts.values = RowBounds.values - 2;
//total number of properties
NumProperties.values = ColBounds.values - 2;
//load the products from the Data source sheet into one range
var ProductRange = MyDataSheet.getRangeByIndexes(3, 1, myBounds.LastRow, 3);
ProductRange.load("values");
})
.then(ctx.sync)
.then(function () {
var ICcode = ConfigSheet.getRange("B36");
ICcode = onzin.values;
//var Mystring = rowAddress.address;
showNotification("onzin waardes: ", onzin.values);
var PropSheet = ctx.workbook.worksheets.getItem("PropertySelection");
PropSheet.activate();
});
}).catch(errorHandler);
I would expect that the worksheet function takes the first 7 characters from the cell value of "Acode" and writes it to the range ICcode on location B36.
any help would be appreciated
Deleted my earlier answer because I misread part of your code.
This line of your code has two problems:
onzin.load("values","address");
There should not be an "s" on the first string. It is just "value". Also, remove the "s" from the line ICcode = onzin.values;.
There is no "address" property on onzin object. (The Excel.Range object does have properties named values and address, which is why I thought in my original answer that you were treating onzin as an Excel.Range object.)
I was able to spot the errors by reading this article in the official documentation: Call built-in Excel worksheet functions. This article is the very first search result in both Bing and Google if you search for "office add-ins worksheet functions". So, I gently don't agree with you that the documentation is "incredibly incomplete".
Will you try this piece, it works on my side.
var Acode = sheet.getRange("B2").load("values, address");
await context.sync();
console.log(Acode);
var onzin = context.workbook.functions.left(Acode.values.toString(), 3);
var substring = Acode.values.toString().substring(0,7);
console.log(Acode.values.toString());
console.log(substring);
Related
Hey I am trying to place arrays of data into a Google Sheet. I've got a list of dimensions which I am mapping through a function calling an external API to get numerical values for each dimension. I then want to input these into my spreadsheet - however I am unsure how to do this. I would also like to input the specific Date into a column as well. So ideal output would be column A = date, column B = dimensions, column C = numerical value.
function function() {
var dimensions = ["CN","IN","NZ","US","UK","MY","SG","JP","KR","DE"];
var counts = dimensions.map(dim => [getValue(dim)]);
}
function getValue(dim) {
// ive removed the detail for the API call
var response = UrlFetchApp.fetch(fetchUrl, params);
var json = JSON.parse(response.getContentText());
var dataSet = json.results;
Logger.log(dataSet);
return dataSet;
}
In your script, for example, how about the following modification?
From:
function function() {
var dimensions = ["CN","IN","NZ","US","UK","MY","SG","JP","KR","DE"];
var counts = dimensions.map(dim => [getValue(dim)]);
}
To:
function sample() {
var dimensions = ["CN","IN","NZ","US","UK","MY","SG","JP","KR","DE"];
var date = new Date();
var counts = dimensions.map(dim => [date, dim, getValue(dim)[0].aggregations[0].value]);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheet name.
sheet.getRange(sheet.getLastRow() + 1, 1, counts.length, counts[0].length).setValues(counts);
}
function cannot be used as the function name.
From your showing log, I thought that your expected value from the URL might be dataSet[0].dimensions[0].value].
From ive removed the detail for the API call, I cannot see your whole script. In your script, fetch is used in a loop. For example, when you use fetchAll method, the process cost might be able to be reduced. But, in this case, it is required to also modify getValue function.
If you want to use this script as the custom function, how about the following script? By this, you can retrieve the result values by putting =sample() to a cell.
function sample() {
var dimensions = ["CN","IN","NZ","US","UK","MY","SG","JP","KR","DE"];
var date = new Date();
var counts = dimensions.map(dim => [date, dim, getValue(dim)[0].aggregations[0].value]);
return counts;
}
This question already has an answer here:
Freezing rows in Sheets with Google Apps throws type error
(1 answer)
Closed 2 years ago.
I'm trying to make my code sending an email by referring to my google sheet data. Im using Apps Script and here is the code. However, as I run my function "sendEmail()", I got "typeError: "cannot read property "1" of undefined(line 17)".
Code line 17
var currentEmail = rows[i][1];
Here is the full code.
var ss = "1kuTkOuCd-wKTS2564oHdxALFbFo-IeyjzToYYhB6NrQ";
var SheetName = "FormResp";
function getRows(){
var rangeName = 'FormResp!A2:E';
var rows = Sheets.Spreadsheets.Values.get(ss, rangeName).values;
return rows;
}
function sendEmails() {
var ss_1 = SpreadsheetApp.openById(ss);
var sheet = ss_1.getSheetByName(SheetName);
var lr = sheet.getLastRow();
for (var i = 0;i<=lr;i++){
rows=getRows();
var currentEmail = rows[i][1];
var startingdate = rows[i][3];
var endingdate = rows[i][4];
MailApp.sendEmail(currentEmail,"Thank You for Applying Leave via Leave form: your request leave starting" + startingdate + "until" + endingdate,"Hello");
}
}
function testgetrow(){
var nama = getRows();
var x = "";
}
I do make a test function "testgetrow()" to check my data, and I do manage to run the function without any error and I do confirm that there is values in my getRows() function.
my getRows() function working, and there is a value in the array as shown in the picture below.
I suppose you can do it any way you wish but this seems a lot simpler to me.
function sendEmails() {
const ss = SpreadsheetApp.openById("1kuTkOuCd-wKTS2564oHdxALFbFo-IeyjzToYYhB6NrQ");
const sh = ss.getSheetByName('FormResp');
const rg = sh.getRange(2,1,sh.getLastRow()-1,5);
const vs=rg.getValues();
vs.forEach(r=>{
var currentEmail = r[1];
var startingdate = r[3];
var endingdate = r[4];
MailApp.sendEmail(currentEmail,"Thank You for Applying Leave via Leave form: your request leave starting" + startingdate + "until" + endingdate,"Hello");
});
}
Answer
The main problem is that you have not declared properly the variable rows in your line 16 rows=getRows(); because you forgot to use the keyword var. Change that line with var rows = getRows() and try it again.
Take a look
You are mixing SpreadsheetApp with Sheets API. I recommend you to pick one and stay there to have a clear and less confusing code.
Try use less functions if they are not really necessary, as #Cooper suggested, you can define the variable rows with getRange that can handle A1 notation ranges and getValues.
Define properly your range in A1 notation: if you write A2:E, your range goes from column A, row 2 until column E, row 1000. You have to add the number of the last row in your range, for example A2:E10.
With the last change, you do not have to calculate the last row, you can simply use rows.length.
It is not necessary to have the id of the spreadsheet if your script is a Container-bound Scripts and not a Standalone Script with getActiveSpreadsheet
I attach you a snippet of the code:
var sheetName = "FormResp";
var spreadsheet_id = "1kuTkOuCd-wKTS2564oHdxALFbFo-IeyjzToYYhB6NrQ";
var ss = SpreadsheetApp.openById(spreadsheet_id).getSheetByName(sheetName)
function sendEmails() {
var rangeName = 'A1:B9';
var rows = ss.getRange(rangeName).getValues()
for (var i = 0; i <= rows.length; i++){
var currentEmail = rows[i][1];
var startingdate = rows[i][3];
var endingdate = rows[i][4];
MailApp.sendEmail(currentEmail,"Thank You for Applying Leave via Leave form: your request leave starting" + startingdate + "until" + endingdate,"Hello");
}
}
I hope that it helps you!
References
SpreadsheetApp
Sheets API
getRange
getValues
getActiveSpreadsheet
Container-bound Scripts
Standalone Script
I'm working on a project that take "profiles" stored in a Google Sheet, makes a unique Google Doc for each profile, and then updates the unique Google Doc with any new information when you push a button on the Google Sheet.
I have some other automations built into my original code, but I simplified most of it to what's pertinent to the error I'm getting, which is this:
Exception: Document is missing (perhaps it was deleted, or you don't have read access?
It happens on Line 52 of my script in the fileUpdate funtion. Here's the appropriate line for reference:
var file = DocumentApp.openById(fileName);
And this is the rest of my code:
function manageFiles() {
//Basic setup. Defining the range and retrieving the spreadsheet to store as an array.
var date = new Date();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var array = sheet.getDataRange().getValues();
var arrayL = sheet.getLastRow();
var arrayW = sheet.getLastColumn();
for (var i = 1; i < arrayL; i++) {
if (array[i][arrayW-2] == "") {
//Collect the data from the current sheet.
//Create the document and retrieve some information from it.
var docTitle = array[i , 0]
var doc = DocumentApp.create(docTitle);
var docBody = doc.getBody();
var docLink = doc.getUrl();
//Use a for function to collect the unique data from each cell in the row.
docBody.insertParagraph(0 , "Last Updated: "+date);
for (var j = 2; j <= arrayW; j++) {
var colName = array[0][arrayW-j];
var data = array[i][arrayW-j];
if (colName !== "Filed?") {
docBody.insertParagraph(0 , colName+": "+data);
}
}
//Insert a hyperlink to the file in the cell containing the SID
sheet.getRange(i+1 , 1).setFormula('=HYPERLINK("'+docLink+'", "'+SID+'")');
//Insert a checkbox and check it.
sheet.getRange(i+1 , arrayW-1).insertCheckboxes();
sheet.getRange(i+1 , arrayW-1).setFormula('=TRUE');
}
else if (array[i][arrayW-2] !== "") {
updateFiles(i);
}
}
sheet.getRange(1 , arrayW).setValue('Last Update: '+date);
}
//Note: I hate how cluttered updateFiles is. I'm going to clean it up later.
function fileUpdate(rowNum) {
//now you do the whole thing over again from createFiles()
//Basic setup. Defining the range and retrieving the spreadsheet to store as an array.
var date = new Date();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var array = sheet.getDataRange().getValues();
var arrayL = sheet.getLastRow();
var arrayW = sheet.getLastColumn();
//Collect the data from the current sheet.
var fileName = array[rowNum][0];
var file = DocumentApp.openById(fileName);
//retrieve the body of the document and clear the text, making it blank.
file.getBody().setText("");
//Use a for function to collect the the unique date from every non-blank cell in the row.
file.getBody().insertParagraph(0 , "Last Updated: "+date);
for (var j = 2; j <= arrayW; j++) {
var colName = array[0][arrayW-j];
var data = array[rowNum][arrayW-j];
file.getBody().insertParagraph(0 , colName+": "+data);
}
}
If you'd like to take a look at my sample spreadsheet, you can see it here. I suggest you make a copy though, because you won't have permissions to the Google Docs my script created.
I've looked at some other forums with this same error and tried several of the prescribed solutions (signing out of other Google Accounts, clearing my cookies, completing the URL with a backslash, widening permissions to everyone with the link), but to no avail.
**Note to anyone offended by my janky code or formatting: I'm self-taught, so I do apologize if my work is difficult to read.
The problem (in the updated code attached to your sheet) comes from your URL
Side Note:
In your initial question, you define DocumentApp.openById(fileName);
I assume your realized that this is not correct, since you updated
your code to DocumentApp.openByUrl(docURL);, so I will discuss the
problem of the latter in the following.
The URLs in your sheet are of the form
https://docs.google.com/open?id=1pT5kr7V11TMH0pJea281VhZg_1bOt8YDRrh9thrUV0w
while DocumentApp.openByUrl expects a URL of form
https://docs.google.com/document/d/1pT5kr7V11TMH0pJea281VhZg_1bOt8YDRrh9thrUV0w/
Just adding a / is not enough!
Either create the expected URL manually, or - much easier / use the method DocumentApp.openById(id) instead.
For this, you can extract the id from your URL as following:
var id = docURL.split("https://docs.google.com/open?id=")[1];
var file = DocumentApp.openById(id)
I have a google spreadsheet data in form of array. Now I want to push the array position I used for loop which is working fine on small data but when the data length is increased it result in delay.
Is there a faster way to push the array position in the array.
Here is the code which I am currently using:-
var ss = SpreadsheetApp.openById('19zxxxxxxxxxxxxxxxxxxxxxxxxOI');
var sheet1 = ss.getSheetByName('Sheet2');
var data = sheet1.getRange("A:H").getValues();
var email = Session.getActiveUser().getEmail();
for(var i = 0; i < data.length; i++)
{
data.unshift(i+1);
} // this for loop takes too much time.
data = data.filter(function(item){return item[7] == email});
var x = data.map(function(val){
return val.slice(0, -7);
})
Logger.log(x)
return x;
}
I believe your goal as follows.
If data.unshift(i+1) is data[i].unshift(i+1) as TheMaster's comment, you want to retrieve the values of the column "A" when the value of column "G" is the same with email. At that time, you want to add the row number to the 1st index of the row value.
From your script, I understood like this.
You want to reduce the process cost of this situation.
For this problem, how about this solution?
Pattern 1:
In this pattern, your script is modified. In this case, the result values are retrieve by one loop.
Sample script:
function myFunction() {
var ss = SpreadsheetApp.openById('19zxxxxxxxxxxxxxxxxxxxxxxxxOI');
var sheet1 = ss.getSheetByName('Sheet2');
var data = sheet1.getRange("A1:H" + sheet1.getLastRow()).getValues(); // Modified
var email = Session.getActiveUser().getEmail();
const res = data.reduce((ar, [a,,,,,,g], i) => { // Modified
if (g == email) ar.push([i + 1, a]);
return ar;
}, []);
Logger.log(res)
return res;
}
Pattern 2:
In this pattern, as other method, TextFinder and Sheets API are used. In this case, the size of base data by searching email with TextFinder can be reduced. And each values are retrieved by one API call using Sheets API.
Sample script:
Before you use this script, please enable Sheets API at Advanced Google services.
function myFunction() {
const spreadsheetId = '19zxxxxxxxxxxxxxxxxxxxxxxxxOI';
const ss = SpreadsheetApp.openById(spreadsheetId);
const sheet = ss.getSheetByName('Sheet2');
const email = Session.getActiveUser().getEmail();
// 1. Retrieve the ranges of rows by searching "email" at the column "G".
const ranges = sheet.getRange("G1:G" + sheet.getLastRow()).createTextFinder(email).findAll();
// 2. Create an object for using with Sheets API.
const reqs = ranges.reduce((o, e) => {
const row = e.getRow();
o.rows.push(row);
o.ranges.push("A" + row);
return o;
}, {rows: [], ranges: []});
// 3. Retrieve values and add the row number.
const res = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {ranges: reqs.ranges})
.valueRanges
.map((e, i) => ([reqs.rows[i], e.values[0][0]]));
Logger.log(res)
return res;
}
If email is included other string, please use matchEntireCell(true) to TextFinder.
References:
reduce()
Advanced Google services
Method: spreadsheets.values.batchGet
I have (what I think is) a simple Google Apps Script that takes parameters from the URL and uses them to write data to a spreadsheet based on whether two of the cells in a row match two of the parameters:
function doGet(e) {
var email = e.parameter.email;
var originalTitle = e.parameter.originalTitle;
var newTitle = e.parameter.newTitle;
var newAdditionalAuthors = e.parameter.newAdditionalAuthors;
var newTechnicalSessions = e.parameter.newTechnicalSessions;
var newApproval = e.parameter.newApproval;
var sh = SpreadsheetApp.openById("18vghxzzt27FcBOvoG8kIFFtswMB7XTsMRg60DJ6_-QM").getSheetByName("Responses");
var data = sh.getDataRange().getValues(); // read all data in the sheet
for (n=0;n<data.length;++n){
if (data[n][2] == email && data[n][3] == originalTitle){ //Find all rows where the email matches the submitter's and the title matches the original
data[n][3] = newTitle;
data[n][31] = newAdditionalAuthors;
data[n][33] = newTechnicalSessions;
data[n][34] = newApproval;
};
sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
}
return ContentService.createTextOutput("Success. You may close this tab.");
}
However, it seems to take a rather ridiculous amount of time for the script to run, and it just gets worse the more entries I have in the sheet. Can someone tell me what I'm doing wrong?