In the following code snippet there is a very simple code.
When I try to use it I'm getting
ReferenceError: ADDRESS is not defined error.
I'll appreciate very much if someone could shed a light on this issue.
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange(ADDRESS(15,D1)).activate;
};
Issue 1:
ADDRESS is a google sheets function but you are trying to use it in Google Apps Script.
The latter does not accept google sheets formulas. It has its own documentation and only JavaScript is supported.
Issue 2:
To execute a function in any programming language you need to add parentheses at the end of the function. Replace activate with activate().
Solution:
You most likely want to take the range of cell D15. If that's the case then simply do:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D15').activate();
};
Keep in mind this will work for the active sheet only (the sheet that is currently selected). If you want to select a specific sheet by its name, then do that and change Sheet1 to the sheet name of your choice:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Sheet1') // put the sheet name of your choice
sheet.getRange('D15').activate();
};
Updated answer based on your comment:
Assuming cell D15 contains a cell reference of the cell you want to activate, then do that:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Sheet1') // put the sheet name of your choice
sheet.getRange(sheet.getRange('D15').getValue()).activate();
};
or based on your original code:
function temp() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange(spreadsheet.getRange('D15').getValue()).activate();
};
Related
I created a Google sheet workbook with 4 sheets. Each sheet will need the following columns to be cleared each Friday (columns C3:S40 and column U3:U40). Here is what I came up with but it doesn't work all the way through. So I was wondering if anyone can help me to simplify the script. Thank you in advance!
function ClearCells()
var sheet = SpreadsheetApp.getActive().getSheetbyName ();
sheet.getRange(C3:S40).clearContent();
sheet.getRange(U3:U40).clearContent();}
Try this:
function ClearRangesofSelectedSheets() {
const shts = ["Sheet1","Sheet2","Sheet3"];
const ss = SpreadsheetApp.getActive();
shts.forEach(name => {
const sheet = ss.getSheetByName(name);
sheet.getRange("C3:S40").clearContent();
sheet.getRange("U3:U40").clearContent();
});
}
I believe your goal is as follows.
You want to simplify your showing script.
As the additional function, you want to use your script for the specific 4 sheets in a Google Spreadsheet.
Modification points:
It seems that your function of function ClearCells() is not enclosed by {}.
getSheetbyName () of SpreadsheetApp.getActive().getSheetbyName () is required to be getSheetByName("Sheet name").
When getRange method of Class Sheet is used, C3:S40 and U3:U40 of sheet.getRange(C3:S40).clearContent() and sheet.getRange(U3:U40).clearContent() are required to be given as the string value as follows.
sheet.getRange("C3:S40").clearContent();
sheet.getRange("U3:U40").clearContent();
In your script, sheet.getRange("C3:S40").clearContent() and sheet.getRange("U3:U40").clearContent() can be modified by one call as follows.
sheet.getRangeList(["C3:S40", "U3:U40"]).clearContent();
In order to use your script to the specific 4 sheets, it is required to give the sheets.
When these points are reflected in your script, how about the following modification?
Modified script:
function ClearCells() {
var sheetNames = ["Sheet1", "Sheet2",,,]; // Please set the sheet names you want to use.
var sheet = SpreadsheetApp.getActive();
sheetNames.forEach(sheetName =>
sheet.getSheetByName(sheetName).getRangeList(["C3:S40", "U3:U40"]).clearContent()
);
}
Or, in order to simplify the script, you can also use the following script. When you use this script, please enable Sheets API at Advanced Google services.
function ClearCells() {
var sheetNames = ["Sheet1", "Sheet2",,,]; // Please set the sheet names you want to use.
var ss = SpreadsheetApp.getActive();
Sheets.Spreadsheets.Values.batchClear({ ranges: sheetNames.flatMap(s => ["C3:S40", "U3:U40"].map(r => `'${s}'!${r}`)) }, ss.getId());
}
I think that when Sheets API is used, the process cost might be able to be reduced a little. Ref
References:
getRangeList(a1Notations) of Class Sheet
Method: spreadsheets.values.batchClear
forEach()
map()
in my script , I am attempting the execute my code on a specific tab.
Here is my code snippet
function sendalert() {
var sheet = SpreadsheetApp.getActiveSheet();
My google sheet has 6 tabs, but I would like my script to run always on the tab named "Operator". How do I enforce this?
I believe your goal as follows.
You want to run the script for the sheet with the sheet name of Operator in the active Spreadsheet.
For this, how about this answer?
Modification points:
In order to access to the specific sheet using the sheet name, you can use getSheetByName(name).
When getActiveSheet() is used, the opened sheet in the browser is used. If the browser is not opened, the sheet of 1st tab is used.
Modified script:
When your script is modified, please modify as follows.
function sendalert() {
var sheetName = "Operator";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
// do something.
}
References:
getActiveSpreadsheet()
getSheetByName(name)
I manage a Google spreadsheet that has multiple sheets. I use scripts to automate some processes on the first sheet. Recently, a user accidentally renamed the first sheet -so the script stopped working until I found out and corrected the name of the sheet.
My question is: how can I protect against this happening again? Is there a way to protect the sheet name from being changed by users other than myself? Or, in the script, is there a different way to reference the first sheet, regardless of its name? I.e., instead of sheet.getSheetName is there something like sheet.getFirst that returns the first sheet, regardless of its name?
Here's a sample of one script that is being affected.
function onOpen(){
//Sheet where this script should run
var SHEETNAME = "Scan IN Check OUT"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if (sheet.getSheetName() == SHEETNAME ) { //Check we are on the correct sheet
var sheet = SpreadsheetApp.getActiveSheet();
//Find the last cell with data in that specific column (A in this case)
var lastCell = sheet.getRange('A1').getNextDataCell(SpreadsheetApp.Direction.DOWN);
//Activate the next cell
lastCell.offset(1, 0).activate();
}
}
SpreadsheetApp.getActive().getSheets()[0]; will always be the first sheet on the left which may not always be the same sheet since users can change their position. You can use the first function below to see how the indexes change as you move sheets around. WARNING: don't do this in a spreadsheet that contains important information because it first clears the sheet and then writes the name, the id's and the indexes for all sheets.
So you can also use the first function to get the id of the sheet that you want not to change.
The second function will get the sheet by id. If you will select the id of your sheet and replace the string 'My Sheet ID' with it and also put the correct name of your sheet where it says my sheet name, then when you runTwo() it will move you sheets all the way to left (ie index 1) and rename to whatever you want.
function runOne() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var sht=ss.getSheets();
sh.clear();
sh.appendRow(['Name','id','index']);
sht.forEach(function(s){
sh.appendRow([s.getName(),s.getSheetId(),s.getIndex()]);
});
}
function getSheetById(id) {
var id=id||'My Sheet ID';
var ss=SpreadsheetApp.getActive();
var shts=ss.getSheets();
for(var i=0;i<shts.length;i++) {
if(shts[i].getSheetId()==id) {
return shts[i];
}
}
}
function runTwo() {
var ss=SpreadsheetApp.getActive();
var sh=getSheetById().activate();
ss.moveActiveSheet(1);
sh.setName('My Sheet Name');
}
Of course the real problem here is that if the user can change the name of your sheet they can also go in and change your script.
I'm trying to be able to access information on the active sheet, but I can't even seem to "connect" with the sheet to begin with. I'm just wondering why this small function isn't returning anything.
I've played around with different ways to call the sheet (e.g. getActive(), getActiveSheet(), getSheetByName(), etc.), and changing the name of the sheet itself.
function CLICK (){
var sheetName = SpreadsheetApp.getActiveSheet();
return sheetName;
}
I just want it to return the sheet name so I know it's accessing the sheet properly.
For your var please try:
sheetName = SpreadsheetApp.getActiveSheet().getName();
Hi there are some different options you can choose depending on what you want to do. I would suggest to read https://developers.google.com/apps-script/guides/sheets. Here are the examples I have for you.
function myFunction() {
/* To work on the first sheet sheet of the Spreadsheet, use getName(); to get the name of the spreadsheet*/
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
/* To get the name of the active sheet*/
var sheet2 = SpreadsheetApp.getActiveSheet().getName();
/* To add values on the active cell (current selected cell)*/
var sheet3 = SpreadsheetApp.getActiveRange().setValue(sheet2);
/*To work on the sheet with a specific Name*/
var sheet4 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
console.log(sheet2);
console.log(sheet4);
/*The difference between getSheets()[0] and getSheetByName("name") is that getSheets()[0]
will always work with the first sheet even if you change position and getSheetByName() will
always work with the sheet you named even if you change position, the position is 0 based*/
}
Here is another function to get name, url and id of the srpeadsheet.
function getDetails(){
var getName = SpreadsheetApp.getActiveSpreadsheet().getName();
var getUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var getId = SpreadsheetApp.getActiveSpreadsheet().getId();
console.log(getName);
console.log(getUrl);
console.log(getId);
}
Remember that if you are using a script editor that is attached to the Spreadsheet for sure you will be working on that spreadsheet. Now if you created a new script directly from script.google.com then you will need to first open the spreadsheet by using an Id or a url to open that specific spreadsheet.
I hope this helps.
I just want a "hello world" and after an embarrassing number of hours, its still not working. I'm new to Javascript and Google App script.
Here is the simple script that fails to enter the "Hello" and I would appreciate any tips you have on getting this to work.
As you can see in the commented lines, I've tried many ways to get my "Hello" out there, but none of them work yet. The menu does work. Thanks for your help
Really what I want to do is to be able to move from cell to cell in a spread sheet, get the values there, assign it to a variable. And then to write the var value to a cell after a condition is meet in the script.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Generate Coin Trades', functionName: 'GCTrades_'}
];
ss.addMenu('CoinTrade', menuItems)
function GCTrades_() {
var ss = SpreadsheetApp.getActive();
SpreadsheetApp.setActiveSheet(ss.getSheetByName('Prices'));
sheet.getRange("$H$1").setValue('Hello');
//settingsSheet.activate();
// One before the last row that has been entered in the spreadsheet
// var LastRow = sheet.spreadsheet.getLastRow() -1;
// var cell1 SpreadsheetApp.getActiveSheet().getRange(LastRow,16);
// var cell = spreadsheet.getRange("$H$1");
// cell.setvalue('Hello')
//var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// var cell = sheet.getRange(1655,16);
// var cell = 'H1655'
// cell.setValue("Hello");
//var count = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4)
// Spreadsheet.getActiveSheet().getRange(LastRow,16).setvalue('Hello')
// SpreadsheetApp.getActiveRange().setValue('hello')
}
There are a couple of typos preventing your script from running. First, your onOpen script isn't closed.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Generate Coin Trades', functionName: 'GCTrades_'}
];
ss.addMenu('CoinTrade', menuItems)
} // missing closing curly brace.
You used sheet.getRange()... but didn't define sheet anywhere. You also do not need to use setActive to edit the sheet, as you can do that on a cell-by-cell basis. It is enough to define the sheet in the script.
Once your sheet is defined, you can get ranges and set the values. I kept your absolute range ($H$1) for the first cell. We then use a relative reference for the second cell by getting the position using the .getRow() and .getColumn() methods.
function GCTrades_() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1')
var cell1 = sheet.getRange("$H$1").setValue('Hello');
var cell2 = sheet.getRange(cell1.getRow(), cell1.getColumn()+1).setValue('World');
}
Try this:
function onOpen()
{
SpreadsheetApp.getUi().createMenu('CoinTrade')
.addItem('Generate Coin Trades', 'GCTrades_')
.addToUi();
}
//This is a private function so it will not show up in script editors function list. If you want it to show up on the function list then remove the trailing underscore
function GCTrades_()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Prices').activate();//The activate is not required but it will force this sheet to be on top
sh.getRange("A1").setValue('Hello');//Do not use $s in the range here
}
What I really want:
Really what I want to do is to be able to move from cell to cell in a
spread sheet, get the values there, assign it to a variable. And then
to write the var value to a cell after a condition is meet in the
script.
It's not clear to me what you "really want". Perhaps, you can begin that code yourself and refine the description of what you "really want".