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)
Related
I want to copy some data from sheet 1 to sheet 2, but every time I tried to run the below-attached code it send me an error which I mention in the comment.
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet 1");
var pasteSheet = ss.getSheetByName("Sheet 2");
// get source range
var source = copySheet.getRange(2,1,38,17);
// get destination range, "TypeError: Cannot call method "getRange" of null."
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,38,17);
// copy values to destination range
source.copyTo(destination,{contentsOnly: true});
}
Thanks in advance.
Your code seems correct and is working for me,
Can you try to run this code and check if its logging the correct name of your google sheet that you are using,
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getName());
}
For better access to a particular google sheet you can use the following method to access the file rather than using getActiveSpreadsheet
var ss = SpreadsheetApp.openById("your sheet id");
You can even try the debug window to check what is the present values of different variables and get some idea of possible errors.
The error you are receiving is due to the fact that pasteSheet is null.
This is most likely due to one of the following reasons:
your spreadsheet does not have any sheet named Sheet 2 - I suggest you double check the name of the sheets from your spreadsheet;
the active spreadsheet you are retrieving is not the one you are needing for copying the data;
As suggested above, you can indeed make use of the openById method in order to retrieve the needed spreadsheet and afterwards get the actual sheets.
Reference
openById(id);
Apps Script Troubleshooting.
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();
};
I do have two different google sheet, I need to copy say sheet Test from one to another using script, but getting error, any thought on this?
var source_sheet= "https://address of the otgoogle sheet tho be copied over")"
//here I'm on another google sheet, add a menu to copy the source from above
var ui = SpreadsheetApp.getUi();
//var ss = SpreadsheetApp.getActiveSpreadsheet();
var result = ui.prompt(
'Add New Sheet',
'Please enter Name',
ui.ButtonSet.OK);
var button = result.getSelectedButton();
var text = result.getResponseText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.copyTo(link_to_src).setName(text);
Getting below error:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.copyTo. (line xx, file "copy-rename")DetailsDismiss
Any help will be appreciated in advance..
You want to copy a sheet of the source Spreadsheet, which is not the active Spreadsheet to the active Spreadsheet, which run the script.
source_sheet is the URL of Spreadsheet like https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit.
When you run the script, you want to input the sheet name in source Spreadsheet using a dialog and copy the sheet to the active Spreadsheet.
I could understand like above. If my understanding is correct, how about this modification?
Modified script
Please modify your script as follows. Before you run the script, please set the destination Spreadsheet URL to source_sheet.
From:
var sheet = ss.getActiveSheet();
sheet.copyTo(link_to_src).setName(text);
To:
var sourceSheet = SpreadsheetApp.openByUrl(source_sheet).getSheetByName(text);
if (sourceSheet) {
sourceSheet.copyTo(ss).setName(text).activate();
} else {
ui.alert(text + " sheet was not found.");
}
When you run the script, a dialog is opened. When a sheet name of the source Spreadsheet is inputted, the sheet is copied to the current active Spreadsheet, then, the copied sheet is activated.
If the sheet name which is not included in the source Spreadsheet is inputted, a dialog is opened.
References:
openByUrl(url)
copyTo(spreadsheet)
I'm trying to copy / transfer a sheet from one spreadsheet to another spreadsheet. I have tried various methods and .copyTo seems to be the best and most efficient way.
.copyTo works well but I'm struggling to send to a specific sheet...
Here is my code:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetA.copyTo(destination); // I tried renaming .setName(each);
}
So if I only use sheetA.copyTo(destination); it simply creates a copy sheet, like Copy of the_souce_sheet_name.
If I try renaming to make it a specific name I will get error after running for the second time that the sheet already exists in destination spreadsheet.
What I really need to achieve is that the function from source spreadsheet copies data from the source sheet to always the same sheet in destination spreadsheet.
Perhaps .copyTo is not the right way to do it?
Any suggestions and code will help please!
The reason for receiving data on a exact sheet in the destination spreadsheet is because I have a trigger On change that executes another script to work with the new incoming data.
You want to overwrite the source sheet of Spreadsheet A to the destination sheet of Spreadsheet B.
You want to keep the sheet name of var each = "Data_Incoming".
If my understanding is correct, how about this answer? I would like to propose 2 samples. So please select one of them for your situation.
Sample script 1:
The flow of this sample script is as follows.
each sheet of Spreadsheet B is deleted.
Source sheet of Spreadsheet A is copied to the destination sheet of Spreadsheet B.
Sheet name of the copied sheet of Spreadsheet B is modified to each.
Modified script:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destination.deleteSheet(destSheet);
}
sheetA.copyTo(destination).setName(each);
}
Sample script 2:
The flow of this sample script is as follows.
Copy the source sheet of Spreadsheet A to Spreadsheet B.
If the sheet with the sheet name of each is not existing in Spreadsheet B, the copied sheet is renamed to each.
If the sheet with the sheet name of each is existing in Spreadsheet B, the source sheet is copied to the Spreadsheet B as Copy of ###.
each sheet is cleared.
All values, formulas and formats of the copied sheet are copied to each sheet.
Delete the copied sheet.
Modified script:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
}
Note:
In this sample script, each sheet of the Spreadsheet B (destination Spreadsheet) is deleted. So please be careful this.
So at first, as a test, I recommend to use a sample Spreadsheet.
References:
copyTo(spreadsheet) of Class Sheet
copyTo(destination) of Class Range
deleteSheet(sheet)
If I misunderstood your question and this was not the result you want, I apologize.
Added:
You want to run the script of destination Spreadsheet when the source values are copied to the destination Spreadsheet using the script in the source Spreadsheet.
The following sample script is for achieving above situation.
Script of destination Spreadsheet:
At first, the script of destination Spreadsheet is prepared.
Script:
function doGet() {
sample(); // This is the function that you want to run when the source values are copied.
return ContentService.createTextOutput();
}
After copy and paste above script to the script editor of destination Spreadsheet, please do the following flow.
Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
Select "Anyone, even anonymous" for "Who has access to the app:".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Copy "Current web app URL:".
It's like https://script.google.com/macros/s/#####/exec.
Click "OK".
Script of source Spreadsheet:
As the next step, the script of source Spreadsheet is prepared as follows. In this sample, the sample 2 was used. I think that you can also use the sample 1.
Script:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
// Added
var url = "https://script.google.com/macros/s/###/exec"; // Please set the retrieved URL of Web Apps.
UrlFetchApp.fetch(url);
}
Note:
By above settings, when TransferDataOut() of the source Spreadsheet was run, doGet() of the destination Spreadsheet is run by UrlFetchApp.fetch(url).
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
I want my spreadsheet to open at a certain sheet. I've tried the following but it still just opens the first sheet.
function setActiveSheetToday(){
var date = new Date();
var sheet = getSheetByDate(date); //custom function that returns sheet object
var SS = sheet.getParent();
SS.setActiveSheet(sheet);
}
Then I make an onOpen trigger.
ScriptApp.newTrigger('setActiveSheetToday').forSpreadsheet(SS_ID).onOpen().create();
When I open the spreadsheet, it still opens at the first sheet. Any suggestions?
EDIT
I think I found the reason why it doesn't work here: https://developers.google.com/apps-script/guides/bound#special_methods
So it apparently only works for bounded scripts..
You could make the sheet you want to open on the first sheet. Just add SS.moveActiveSheet(1); to your setActiveSheetToday() function.
Alternatively, activating a sheet from a bound script changes the focus as you want. Doing it from a separate script file doesn't appear to.
Have you checked the object, your sheet?
You may try this function for simple testing:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
ss.setActiveSheet(sheet);
}
make sheet called "Sheet1", go to any other sheet and reload the page.
for your code I suggest the test:
Logger.log(sheet.getName());