I am a beginning Google sheets script writer, though I've done javascript and coding for years. I am trying to do some simple sorting on arrays, but can't get the UNIQUE or the SORT function to work. Any reference to them and I get:
ReferenceError: "UNIQUE" is not defined.
I'm starting to think I am missing a library or module or something needs to be enabled in sheets. I did enable the Google sheets API, through the Resources tab on the script editor.
Here is my script:
function fcnImportStockData() {
var i=1;
var arrStockSymbols = [];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
// get last cell of the range
while (sheet.getRange("C"+i).getValue() != "" ) {
sheet.getRange("d15").setValue(i);
i++;
}
arrStockSymbols = UNIQUE("C1:C"+i);
}
On a side note, I couldn't get the function ISBLANK to work either, to simplify the While loop looking for the first blank cell in the column to set the range for sorting.
I think I am missing something simple or big picture. I have scoured the web for similar situations, and found nothing much.
TIA!
First, keep in mind that Google Sheets Script is nothing more then Javascript.
UNIQUE() is not a valid JS function, but it's a spreadsheet function that can be used inside a formula.
To use it, you must set a formula into some cell:
SpreadsheetApp.getActiveSheet().getRange("X9").setFormula("=UNIQUE(C1:C" + i + ")");
Related
I came across a problem with my code in Google Apps Script for Sheets. I want to get cell values from 3 different cells, concatenate them in a single string variable, and copy this string variable into a cell of my spredsheet, using the copyto function.
The problem is that Google Apps Script doesn't recognize copyto as a function, because it doesn't work with local string variables (it works fine with other function variables, such as getrange or else). Here is the part of my code that doesn't work :
var prog = f1.getRange("A3");
var jour = f1.getRange("B1");
var heure = f1.getRange("B2");
var texte = prog+" - "+jour+" à "+heure;
heure.copyTo(f2.getRange(1,2))
f1 is properly defined.
Where do I get this wrong ? Is there a workaround for this ?
Cheers
You say that f1 is properly defined and yet this example is complete and works as well.
function copy2test() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const sh2 = ss.getSheetByName('Sheet2');
sh.getRange('A1').copyTo(sh2.getRange('A2'));
}
Your example is incomplete because I cannot copy it and put it into my code editor and reproduce the same result as you get. So you haven't met the requirement for reproducibility. In my own mind I simply look at your example and assume that you don't know what you're talking about when you say it's defined properly.
You placed it in a snippet. So just run it: It returns an error with the following:
{
"message": "Uncaught ReferenceError: f1 is not defined",
"filename": "https://stacksnippets.net/js",
"lineno": 12,
"colno": 22
}
So you snippet result and I both agree. It's not defined properly.
For anyone coming across the same problem as I was, here is my solution. Because copyTo doesn't work with a string variable such as I had (or at least I don't know how to make it work), I used a different method which produces the same result.
Instead of heure.copyTo(f2.getRange(1,2))
I used f2.getRange(1,2).setValue([texte]);
which does what I wanted and work with a string variable.
I'd like to remove my user created label called "Add-to-Spendee-2", from a collection of emails. I've pretty much followed Google's removeLabel() Documentation to the dot on this, but I keep getting an "Invalid argument: label" error.
Here's the code:
function removeLabel()
{
var myLabel = GmailApp.getUserLabelByName('test-add-to-spendee-2');
var threads = GmailApp.search("label:test-add-to-spendee-2 AND from:swiggy AND subject:(Your receipt for Swiggy order)");
for (var x in threads)
{
var thread = threads[x]
thread.removeLabel(myLabel)
}
}
Note: If I substitute the removeLabel(myLabel) with any other function like markUnread(), the code works perfectly.
I think your code will work but I think all you need to do is:
var lbl=GmailApp.getUserLabelByName('Q0/Subject/Name');
var threads=GmailApp.search('label:Q0/Subject/Name');//exactly as you created it
lbl.removeFromThreads(threads);
Try using the debugger and make sure that threads is getting an array of GmailThread objects.
This is what the label look like in the Gmail search window:
They changed the slashes to dashes and used lower case and that's not really what the label looks like.
As I said above in my comment:
I just did that recently and I found that the description of the label in the gmail search window did not agree with how I actually created the label. It displayed a label like this q0-subject-name and I had it created as Q0/Subject/Name when I used q0-subject-name I couldn't find the label and when I used Q0/Subject/Name I found it.
I am using Google Sheets.
Cell A1:
=image("address.jpg")
This puts the image in a cell. To get the url from Stack Overflow produced this answer.
I created the script, and Google recognised it in autocomplete. The error I am getting is:
TypeError: Cannot call method "match" of null. (line 10).
I ran the regex through a checker, and it does get what I am looking for i.e the address, but the error seems to indicate that it's coming back with nothing.
Does this still work? Has Google changed something?
My work-around is to create two sheets and have §=image in one sheet, while in the second sheet, I remove § and use a standard Google function.
The linked solution is far better, and I'd like to implement that if I could. I cannot post a comment on the original solution's page as I don't have reputation points.
In your situation, "null" is given as an argument. Because the formula cannot be directly retrieved by =getImageUrl(A1). By this, the error of Cannot call method "match" of null. occurs. The formula can be retrieved by getFormula(). This is also mentioned at Eric Koleda's answer. So for example, the script can be modified as follows.
Modified script:
function getImageUrl(range) {
var formula = SpreadsheetApp.getActiveSheet().getRange(range).getFormula(); // Added
var regex = /=image\("(.*)"/i;
var matches = formula.match(regex);
return matches ? matches[1] : null;
}
Note:
If =image("URL") is put in "A1", when you use this like =getImageUrl("A1"). Please enclose A1 by the double quotes. By this, the string of "A1" is given to the function and is used as the range.
Reference:
getFormula()
Love the "=IMPORTHTML" function in google sheets. However, having an issue parsing the function in a script that I have written in Google Sheets script. The error I receive a "Formula parse error". When I enter formula within a spreadsheet with the code generated parsed in my script, the function completes as expected.
My code is as follows:
function testsetform() {
var weekno = Browser.inputBox("Enter Week No: ");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
cell.setFormula('=IMPORTHTML(ʺhttp://www.leaguesecretary.com/LeagueBowlerList.aspx?LID=40233&YearNum=2015&Season=f&WeekNum='+weekno+'"'+', ʺtableʺ, 1)';
}
The results of the parse in the cell updated appear as follows:
=IMPORTHTML(ʺhttp://www.leaguesecretary.com/LeagueBowlerList.aspx?LID=40233&YearNum=2015&Season=f&WeekNum=28", ʺtableʺ, 1)")
It appears that the results of the parse are not interpreted as desired. The format of the code looks good but obviously something wrong. I can't see it. Any ideas of what I am doing wrong would be appreciated.
I had assistance from Google support. Another pair of eyes proved invaluable. The code above had missing parenthesis and missing quote. The correct code should be:
cell.setFormula('=IMPORTHTML("http://www.leaguesecretary.com/LeagueBowlerList.aspx?LID=40233&YearNum=2015&Season=f&WeekNum='+weekno+'"'+', "table", 1)');
I'm trying to write a custom function that will count and return the number of cells that contain information on a singular column. I'm using a range of A(x):Z(x) and I can't seem to dynamically pass these so I can get a user defined range everytime.
Edit: Current Error I'm receiving right now says: Missing ) after argument list. (line 10, file "Number Of Updates")
So far I have this....
function updateNum(row1,col1,row2,col2)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("report status");
var r = s.getActiveRange(row1+col1:row2+col2); <---Line with error
return (range.length-2)/2
}
any suggestions on how I could make this pass the range?
thanks,
Alexander
change the line for:
var r= s.getRange(row1, col1, row2-row1+1, col2-col1+1);
but that won't solve your problems has the line just behind is a non sense. "range" is not declared. I don't see where you want to go exactly. please write some pseudocode.
If what you want is to count the number of cells that got informations in it, just use the spreadsheet native function
counta()
Its not possible with custom functions because they are deterministic. That is, they will return the same result given the same input.
In your case even if you fix the bugs you are still not passing the actual input just its range limits. Thus if the data inside the range changes you still pass the same input limits thus will return the previous cached result.
There are ways arround that like passing an extra parameter that always changes like 'now()'but that has performance problems as it will recalculate constantly.