setNamedRange() outside of the spreadsheet container? - google-apps-script

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.

Related

How can I make better use of all these information in the debugger?

if I have a simple code like below and I break after this line
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
I will see the variable sheet in the debugger. If I expand it, I will see many functions such as getName. If I expand getName, I will see information such as name: "".
I was expecting to see the name of the activeSheet but it was not there. What are these information for and how can I use them in debugging?
I just want to make sure there isn't a better way to debug and I do not know.
Sheet.getName is a function object, and Google has chosen to not reveal the implementation of these methods. The name property of the function would contain the function's name rather than the name of the active sheet, but my recollection is that the V8 engine used by Google Apps Script does not reveal function names through that property in the debugger.
To see the sheet name in the debugger, declare another variable, like this:
const sheetName = sheet.getName();

.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();

Google Sheet Error: Service Spreadsheets timed out while accessing document with id

I get this error "Service Spreadsheets timed out while accessing document with id ..." every time I run a very simple code, in which I am basically copying data from one google sheet to another using getValues() and setValues().
I don't think it is because of 5M cells limit, because the same exact function is working perfectly fine in another Google Sheet with even bigger size. So I really don't understand where the problem is.
I have tried to create an empty GS and run the function, so I am only pulling data without any other calculation, but still, it gives me the same error.
Any idea what the reason could be?
Here the code as reference:
function MyFunction(){
var pm_ss_0 = SpreadsheetApp.openById('...');
var pm_tab_0 = pm_ss_0.getSheetByName('...');
var pm_data_0 = pm_tab_0.getDataRange().getValues();
var target_ss_0 = SpreadsheetApp.getActiveSpreadsheet();
var target_tab_0 = target_ss_0.getSheetByName('...');
target_tab_0.clearContents();
var target_data_0 = target_tab_0.getRange(1, 1, pm_data_0.length,
pm_data_0[0].length).setValues(pm_data_0);
}
I solved the issue inserting a flush before and after the line where the error appeared.
SpreadsheetApp.flush();
ss.insertSheet("Report "+fogl.getName(), ss.getNumSheets()); //line with the error in my code
SpreadsheetApp.flush();
This issue has also been reported on Google's Issue tracker
Go there and star the issue so you get the updates on it.
This problem is more random than 95% of the commentary on the Web about it attests to. I just had this happen to me for the first time, and it even affected a Macro that did absolutely nothing but hide the Active Tab. I couldn't do anything with Script Editor.
I tried simply duplicating the document. BION, that was the end of the problem for me. Or at least, so far.

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.