range.requireValuesInList and requireValuesInRange seem to be broken - google-apps-script

Up to this morning, the following code worked without any problems
function onOpen(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("B2:B100");
var dv = range.getDataValidation();
dv.requireValuesInList(["Shower","Shave","Shop"]);
dv.setShowDropDown(true);
range.setDataValidation(dv);
}
But this evening, both functions give the error message:
TypeError: Cannot find function requireValuesInList in object DataValidation.
TypeError: Cannot find function requireValuesInRange in object DataValidation.
Any idea what happened? are these functions being replaced?
Just to test, I created a new blank spreadsheet, pasted in the onOpen function and tried to run it... no work.
Any insight is greatly appreaciated.

If you print the return value of Object.keys(dv) [listing all the methods of the DataValidation object] you now get:
isAllowInvalidData,setAllowInvalidData,getCriteriaValues,setHelpText,toString,setCriteria,getCriteria
It seems the DataValidation prototype has just changed utterly, with no warning or documentation of the change. Google’s own documentation (http://developers.google.com/apps-script/reference/spreadsheet/data-validation) refers to the now-defunct DataValidation prototype.
Nice one Google!
Waqar, I’m not sure how you can claim getDataValidation() is undocumented. The DataValidation object returned by getDataValidation() was well-documented at the link I posted above.

getDataValidation() method in range object is undocumented so we can't say what happened to this actually. Best practice is to use only those methods and classes which are documented.
Here is a link for the same in issue tracker. You may check it.
https://code.google.com/p/google-apps-script-issues/issues/detail?id=2356

Related

.clearcontent not working on just one spreadsheet

I have the strangest problem. Just one function seems not to be working on my Spreadsheet. I can setContent in a cell but I cannot clear it.
function buildCrawlGrid(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dynadotS = ss.getSheetByName('Dynadot');
dynadotS.getRange('b5').clearContent;
}
I am very familiar with the use of this function and never had an issue in the past. I have tried in a different sheet in the same spreadsheet and have the same issue. It's not that the spreadsheet object is misnamed because I was able to set content in the cell. I tried SpreadsheetApp.flush() to ensure it wasn't latency. Any ideas what might cause this apart from a corrupt spreadsheet?
I am a bit late...but maybe could be useful for someone reading this.
Try to change with () at the end
dynadotS.getRange('b5').clearContent();

Published google script link with Google Sheet returning printing empty variable

Since I tend to ramble, first a short version and if you need more information read the long one.
TL;DR
Why is this:
function doGet(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var jobsCreated = sheet.getRange(12,2).getValue();
Browser.msgBox(jobsCreated);
var params = JSON.stringify({number:jobsCreated});
return ContentService.createTextOutput(params);
}
returning this when I published as website and then open:
{"number":""}
when it should look more like this {"number":2451}
Full Version:
First of all, I learned to program back in uni for my Computer science degree (10 years ago) but since then I haven't done much programming so I am basically a newbie.
Now to the question. I have a very simple script that a created with the script editor from Google Sheets
function doGet(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var jobsCreated = sheet.getRange(12,2).getValue();
Browser.msgBox(jobsCreated);
var params = JSON.stringify({number:jobsCreated});
return ContentService.createTextOutput(params);
}
First I get the sheet I am working on
Then I select a cell from that sheet
now if I use a msgBox to make sure that I have the right number and run the script, it works and it shows the message.
next, I format the variable as JSON and finally I just create a text output.
Now I deploy as Web app
Execute as ME
Anyone, even anonymous
And when I access the website I can only see this:
{"number":""}
If I change the code and give jobsCreated and static value it works fine
var jobsCreated = 100;
{"number":100}
So my conclusion is that the problem is with accessing the value of the cell when running the script from the published link compare to running it directly from the editor, but I have no idea how to fix this.
A little bit more information, i am trying to use this for a counter called Smiirl, i got most of the information from here
https://medium.com/#m_nebra/bootstrapping-your-company-counter-22f5d4bc7dd4
try this:
function doGet(e) {
return ContentService.createTextOutput(Utilities.formatString('number: %s',SpreadsheetApp.getActiveSheet().getRange(12,2).getValue());
}
Ok as I replied to #Aerials, thank you again for your help btw. Seeing other codes that should work not working with my script, I decided to create a new sheet and script as a test and with the exact same code it works.
But now checking on it a little bit more, something that I didn't think it was a problem since it was getting the number without any problems. The cell it's being populated by a GoogleAnalytics add-on. Now when setting up the add-on again to get the information the script from the website returns an empty value again. SO it seems the issue is with the script getting the information from the sheet (only the published version) when its being populated by the add on
Your issue is in the use of JSON.stringify
In JSON, functions are not allowed as object values.
The JSON.stringify() function will omit or change to null any functions from a JavaScript object.
That said, you can do the following:
function doGet(e){
// Get the value of the range
var sheet = SpreadsheetApp.getActiveSheet();
var jobsCreated = sheet.getRange(12,2).getValue();
// JSON Stringify it
var params = JSON.stringify({"number" : number=jobsCreated});
// Return JSON string
return ContentService.createTextOutput(params);
}
Returns:
{"number":123} if jobsCreated is the number 123, or
{"number":"Mangos"} if jobsCreated is the string "Mangos".
See it the script deployed here, and the sheet to play with.
Note:
You should avoid using functions in JSON, the functions will lose their scope, and you would have to use eval() to convert them back into functions.

How to open filter view without opening a new tab or browser window using Apps Script

I would like to open filter views via button. If I understood it correctly, this should be possible by now. But I'm not sure, because I don't understand the whole communication on the following topic:
https://issuetracker.google.com/issues/36753410)
I have written the following code. When I execute this code, I always get the error: "Method getFilter(object) not found". What
What am I doing wrong?
function OpenFilter(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Name of the sheet'); //The sheet in which I want to open the filter view
var rg = sheet.getRange("S1:S2"); //There is the range where my Hyperlink is placed
var link = rg.getCell(1,1).getValues(); // The cell of the Hyperlink to test
var filter = sheet.getFilter(link);
SpreadsheetApp.setFilter(filter);
}
Unfortunately the method setFilter(filter) has not been implemented so far.
Also, the method getFilter() does not accept parameters, see here.
I recommend you to have a look here to find examples what you can do with filters at current stage.
Keep in mind that you need to combine SpreadsheetApp with the Advanced Sheets Service to achieve your goal.

function onOpen() is not running

My function includes adding a menu and toast to the document. I have verified that the trigger (onOpen) is set as well. It only works when a user goes into Tools, Script Manager, Run. We have too many users with too many backgrounds to expect then to know how to do this. Why isn't it working? (Using Chrome)
function onOpen()
{
var menus = [{name: "Advance in Workflow", functionName:"sendEmail"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Auto Advance FG Workflow", menus);
//sheet.toast(Notify/Remind users);
sheet.toast("While you are here we kindly ask that you do not add, modify or remove any columns.","Welcome - " + username,8);
}
Thanks,
I was having the same issue.
I realized, sometimes Google create some kind of cache of the scripts (I'm used to have a "test" script and I usually alter it's content, and, sometimes, the script runs as if I didn't).
So, what I did that solved the onOpen() not working was changing the function name and ading a trigger manually.
Go to "Resources -> Current script's triggers…"
Choose the function to run on open
It worked like a charm here!
Updated Location Information:
or
Then
This is an old post but I just had this problem and find out why it was not working correctly in my case:
I had, at the top of my script file a variable that required some authorisations and that prevented the script to correctly run. I saw that OP called var username = Session.getActiveUser().getUsername(); (that requires authorisations, and it's may be the cause).
eg:
this code won't work:
function onOpen(){
SpreadsheetApp.getUi()
.createMenu("Exportation")
.addItem("Lancer l'exportation", "exportationMenu")
.addToUi();
}
var stConsCons= SpreadsheetApp.openById(sgcid).getSheetByName("Consultant");
but this one will work:
function onOpen(){
SpreadsheetApp.getUi()
.createMenu("Exportation")
.addItem("Lancer l'exportation", "exportationMenu")
.addToUi();
}
function whatever(){
var stConsCons= SpreadsheetApp.openById(sgcid).getSheetByName("Consultant");
...}
It turns out that you need to add the onOpen(e) function to Triggers!
In my case, onOpen wasn't working because I had a variable, outside of a function, opening a sheet with SpreadsheetApp.openById() rather than SpreadsheetApp.getActiveSpreadsheet(). I guess onOpen doesn't work with openById() even if the sheet you are opening is bound to the script. onOpen() won't work with this kind of a variable outside of a function:
var sheet = SpreadsheetApp.openById("1b_PQD...").getSheetByName("demos")
If your script is bound to the sheet, you can solve this problem by using the getActiveSpreadsheet() function. Otherwise, you can solve it by putting your openById() call into a function.
In my case there was a reference error, that while did not stop the script entirely, it did stop the menu from appearing.
I was only able to detect that error after I run a debug on the script.
It looks like the problem may be that "sheet" isn't defined, which is why the toast is failing.
I know this is a really old question, but for any one finding this now, I may have a solution. The onOpen function often runs with the authorization mode none instead of limited when being used as an event trigger. This may cause errors in things that are related to the specific file or user data. For example:
function onOpen(e) {
SpreadsheetApp.getActivePresentation(); //Will error out if permissions are not set to limited.
SpreadsheetApp.getUi(); //This will always run even if the AuthMode is set to NONE
}
Additionally it is worth noting that if you have any variable used or initialized before onOpen(e), basically any global variables that access sensitive info fail if the AuthMode is set to NONE.
var ss = SpreadsheetApp.getActivePresentation(); //bad
var ss;
...
function init() {
ss = SpreadsheetApp.getActivePresentation(); //good because now that the function is already run we should have full permissions
}
Simple triggers silently fail if they lack permission. I ran into this with an onOpen() in a script that initialized File objects of non-bound files. I moved all File object instantiation to menu functions that do have permission.
In other words, this will not work
function onOpen(e) {
...
let file = DriveApp.getFileById(nonBoundFileId);
...
}
but this will work
function menuFunction() {
...
let file = DriveApp.getFileById(nonBoundFileId);
...
}
because menuFunction can be given permission(s) that simple triggers lack.

setNamedRange() outside of the spreadsheet container?

I've tried as many combinations as I could come up with.
My goal is to have a Google Apps Script running StandAlone or from a Library and be able to set Named Ranges in a spreadsheet.
As best I can figure it, the setNamedRange() method is only available from within the Spreadsheet container and only when you use SpreadsheetApp.getActiveSpreadsheet().
I tried using openById() to no avail. The method is just not available.
Thought I was clever and tried openById then setActiveSpreadsheet. I wasn't clever enough.
Update, I opened issue 1816 "Object become global, auto complete persists even when deleted" with google-apps-script-issues http://code.google.com/p/google-apps-script-issues/issues/detail?id=1816
Quite interesting behavior. Misled me into asking the wrong question
Looks to be a bug in the GAS editor.
The following function demonstrates how to set a named range in a standalone script.
function testNamedRange() {
var ss = SpreadsheetApp.openById('here is the spreadsheet id');
var range = ss.getRange('Sheet1!A1:B2');
ss.setNamedRange('TestRange', range);
var rangeCheck = ss.getRangeByName('TestRange');
var rangeCheckName = rangeCheck.getA1Notation();
}
The rangeCheckName variable contains the A1:B2 string.