How to create a defined named range in google sheets using script - google-apps-script

Is it possible to create named ranges in Google sheets using google script? I would then want the spreadsheet to be able to use these in formulas.

You may want to check out this post:
setNamedRange() outside of the spreadsheet container?
and see if that helps you ..

Class SpreadSheet has the method setNamedRange(name, Range) that can be used.
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setNamedRange("myNamedRange", SpreadsheetApp.getActiveRange());
Link to the Google Documentation:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet?hl=en#setnamedrangename,-range
With me the pitfall was that I thought the method setNamedRange() belonged to the Class Range or Class Sheet. But turns out it belongs to the SpreadSheet Class.

Related

Cannot read property 'getSheetByName' of null

function sortResponses() {
var Sheets = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Fall 01")
sheet.sort(3, false);
}
I have a Sheet called Fall 01 in my google Sheets, where I explicítly had to give the script access to, but it won't open, what am I missing?
Explanation/Issue:
The issue is that you have a standalone script which therefore is not bound to a google spreadsheet and as a result SpreadsheetApp.getActiveSpreadsheet() returns null.
Solutions:
There are two ways you can follow:
Solution 1:
Use SpreadsheetApp.openById(id) or SpreadsheetApp.openByUrl(url):
var Sheets = SpreadsheetApp.openById("Put your Spreadsheet ID").getSheetByName("Fall 01");
or
var Sheets = SpreadsheetApp.openByUrl("Put your Spreadsheet URL").getSheetByName("Fall 01");
Solution 2:
Go to the spreadsheet file that you want to work with and click on Tools => Script editor on the top menu of the spreadsheet file and put your current code there.
Please Note
If this is your only code, sheet is not defined, therefore you will get another error down the road. You most likely want to replace sheet with Sheets or the other way around. Be careful with this.

Google script: How do I find active cell in a different spreadsheet

I have an active cell in an open google sheet. I have a macro in a different spreadsheet that wants to get the value of the target active cell in a different google spreadsheet. Currently when I run the macro it is finding Sheet1 in the target workbook, but not the active sheet. How do I find the active sheet and active cell when I run the macro?
function getFormula() {
//Precedents
var ssThis=SpreadsheetApp.openById("1VMwjomfjtJLIQc<replaced>8J1zWx_GujXuCY");
//Universal Demo - v5
var tss = SpreadsheetApp.openById("1Pev8gCbl_Mn9JMJId<replaced>Cbc0aikBZambI");
var targetSheet=tss.getActiveSheet();
var targetSheetName=targetSheet.getName();
Logger.log(targetSheetName);
var cellThis=targetSheet.getActiveCell();
var targetFormula=cellThis.getFormula();
Logger.log(targetFormula);
getPrecedents(targetFormula);
}
The script doesn’t have a direct relation with the Sheets UI regarding the active cells, sheets, etc, at least that you’re using a container bound spreadsheet and you’re running the script with a macro or add-on inside the spreadsheets UI (as i understand you’re doing). But in this case, you’ll only be able to work with the active cells on the spreadsheet that’s container bound.
To summarize, the functions getActiveSheet() and getActiveCell() will only return the correct value for a container bound spreadsheet, running the script inside the spreadsheet (macro or add-on) and using the “getActive...” functions to obtain the spreadsheet, sheet or range, (instead of openById()) [1]. For example:
var ssThis = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet=ssThis.getActiveSheet();
var cellThis=activeSheet.getCurrentCell();
In any other cases, on the spreadsheet you open, it will always return the first sheet and the first celL (A1). At least you use the “activate()” function [2] or “setActive…” functions [3] to set the active cell or sheet, but for this you would have to know the active cell.
[1] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#getactive
[2] https://developers.google.com/apps-script/reference/spreadsheet/range
[3] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#setactiverangerange

When to use getActiveSpreadsheet?

I'm trying to understand when to use getActiveSpreadsheet().
I've seen numerous examples:
var ss = SpreadsheetApp.getActiveSheet();
But I've also seen:
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
Is the active spreadsheet implied in some circumstances?
Yes, it is implied. The reference for SpreadsheetApp states:
getActiveSheet()
Gets the active sheet in a spreadsheet. The active sheet in a
spreadsheet is the sheet that is being displayed in the spreadsheet
UI.
Including the getActiveSpreadsheet() is probably often done because the getting of a spreadsheet is needed for other sheets and methods and it keeps the command flow consistent when done everywhere.
getActiveSpreadsheet() is a method used by container-bound script.
A script is bound to a Google Sheets, Docs or Forms file if it was created from that document rather than as a standalone script. The file a bound script is attached to is referred to as a "container". Bound scripts generally behave like standalone scripts except that they do not appear in Google Drive, they cannot be detached from the file they are bound to, and they gain a few special privileges over the parent file.
getActiveSpreadsheet(), getActiveDocument(), and getActiveForm() allow bound scripts to refer to their parent file without referring to the file's ID.
In Google Sheets, getActiveSheet(), getActiveRange(), and getActiveCell() let the script determine the user's current sheet, selected range of cells, or selected individual cell. setActiveSheet(sheet) and setActiveRange(range) let the script change those selections.
Taken from https://developers.google.com/apps-script/guides/bound
getActiveSheet() is usefull when you are working with one sheet in the active spreadsheet.
You would use getActiveSpreadsheet() when you need to use other methods for example getSheets() would get all the sheets and you can access them as an array. Or if you wish to work with the spreadsheet as opposed to the sheet itself.
A good example would be the methods available to the spreadsheet class which would not be available for the sheet class

How do I access a particular worksheet in the active spreadsheet in google sheets?

The documentation claims there is a getSheetByName() and a getSheets() but they don't seem to be available anymore...
See https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet
e.g. I'd like to do:
var sheet = SpreadsheetApp.getActiveSheet();
var wks = sheet.getSheetByName('Test');
getSheetByName() and getSheets() are both spreadSheet class methods, not sheet class methods.
I'm afraid you made a consfusion between these two. Btw it seems quite obvious that you can't get a "sheet" from a "sheet" doesn't it ?
You could easily avoid such errors by making use of the integrated autocomplete feature in the script editor. Simply type a period (.) after the class (then use CONTROL SPACE shortcut) name and you'll get all the available methods for this class.

How to access data on different Google Spreadsheet through Google Apps Script?

I'm writing a Google Apps Script on my Google Site and am trying to use data that is provides on 2 different tabs in a Google Spreadsheet. From what I thought I understood from the documentation I could use all available methods in the SpreadsheetApp class on a Sites script by just using the openById() method.
Anyway here is what I tried to do
function doGet(e) {
var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE).getActiveSheet();
SpreadsheetApp.setActiveSheet(doc.getSheetId()[1]);
//....
}
I get the error
Cannot find method setActiveSheet(. (line 4)
I'm pulling my work off this link: Storing Data in Google Spreadsheets and also the Ui Service section listed under Building User Interfaces.
Anybody seeing what I'm doing wrong in these two lines?
setActiveSheet should be used only with the spreadsheet displayed by the UI, a sheet in a spreadsheet you have opened in your browser.
With SpreadsheetApp.openById you are opening a spreadsheet to access its data, but it doesn't open in your browser. It hasn't an UI.
I found this comments in https://developers.google.com/apps-script/class_spreadsheetapp?hl=es-ES#openById :
// The code below opens a spreadsheet using it's ID and gets the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Some examples assume your script is running into your spreadsheet. It's not your case, because you are running a script as a service, which should have its own User Interface.
I think #megabyte1024 addresses the syntax errors, but in answer to your comment to #YoArgentina:
Do you happen to know of a way to access data on different tabs then
through a service not running inside the Spreadsheet?
Does this sort of help?
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheet1A1 = sheets[0].getRange('A1').getValue();
var sheet2A1 = sheets[1].getRange('A1').getValue();
You need to access each sheet separately.
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheet = ss.getSheets()[0]; // "access data on different tabs"
ss.setActiveSheet(sheet);
There is at least one problem in these two lines. The 1st one is that the setActiveSheet method parameters is a Sheet class object and the getSheetId method returns an integer value. By the way this method (getSheetId) is not documented. The 2nd problem can happen if the SpreadsheetApp has no active spreadsheet. In this case there is the "Please select an active spreadsheet first." error. Use the SpreadsheetApp.setActiveSpreadsheet method to set an active spreadsheet.