How to assign two functions to a single button - google-apps-script

I only know how to assign a script to a button in Google Sheets but I don't know how to assign two or three function(scripts) to a single button. That when I press the button in making invoice the invoice change number, saves to next sheet, and print. How could it be.how can i run combine these three scripts at one in google sheets.
function export() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Unwanted Sheet');
sheet.hideSheet();
DriveApp.createFile(ss.getBlob());
sheet.showSheet();
}
function clearRange2() { //replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('A10:F26').clearContent();
}
function clearRange2() { //replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('A10:F26').clearContent();
}
}

Make another function that calls all the functions you need, then assign that function to the button.
Also if you are calling those functions from the same button, maybe those functions should all be combined into one function?

Related

Trigger when change is happening in specific sheet

I have a sheet with data that is imported through IMPORTRANGE to another sheet. When I make changes to Spreadsheet 1 >>> Spreadsheet 2 I have a script that copy the data from Copy to Paste. It is working all fine however I want to make sure that the script only runs when changes are made in the 'Copy' sheet and not any other. At the moment it runs independent on what sheet I make changes in.
Spreadsheet 1
Spreadsheet2
I tried onChange trigger two different ways...
This one makes the change but triggers when changes are made in any of the sheets
function Trigger2(e) {
var sheet = e.source.getActiveSheet();
if (sheet.getName() === 'Copy') {
copyInfo();
}
}
AND
(this does not work)
function Trigger2(e) {
var sheet = e.source.getActiveSheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
if( sheet.getActiveSheet().getName() !== "Copy" ) return;{
copyInfo();
}}
The copy code looks like this...
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Copy");
var pasteSheet = ss.getSheetByName("Paste");
// get source range
var source = copySheet.getRange(2,1,12,8);
// get destination range
var destination = pasteSheet.getRange(2,1,12,8);
// copy values to destination range
source.copyTo(destination);
}
Copy Changes from one Sheet To Another after Imported Range Changes
Spreadsheet 2 Imports a range to Sheet1 from Spreadsheet 1 Sheet1 and when a change occurs in Spreadsheet 2 Sheet1 we copy data from Spreadsheet2 Sheet1 to Spreadsheet2 Sheet 2.
function onMyChange(e) {
//Logger.log(JSON.stringify(e));//useful during setup
//e.source.toast("Entry");//useful during setup
const sh = e.source.getSheetByName("Sheet1")
Logger.log(sh.getName());
if(e.changeType == "OTHER") {
let tsh = e.source.getSheetByName("Sheet2");
sh.getRange(2,1,12,8).copyTo(tsh.getRange(2,1));//Only need the upper left hand corner of the range. Thus you change change the size of the source data without having to change the target range.
}
}
One might be inclined to attempt to use e.source.getActiveSheet() unfortunately this does not necessary get the sheet that you have written unless in is SpreadsheetApp.getActive().getSheets()[0]. onChange event object always returns the most left sheet in the spreadsheet in this situation so it can't be used to identify the sheet that is being written to from the importRange function.
With this function you no longer require another function.
A simple if should work, but you're trying to compare the name to the wrong field. According to Google's documentation, the source field in the Event Object e returns the Spreadsheet object, which is the entire document.
Instead you can use the e.range field, which contains the exact range where the edit was made, and the Range class has a getSheet() method to get the parent Sheet.
So you can modify your sample to the following:
function Trigger2(e) {
var sheet = e.range.getSheet();
if (sheet.getName() === 'Copy') {//Edit: this will only work if sheet is the most left sheet in the spreadsheet
copyInfo();
}
}

GoogleSheets ApsScript ReferenceError: ADDRESS is not definedDetails

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

Deleting specific data from multiple Google Sheets

I am trying to clear data from multiple sheets. I have a code that works for the current active sheet and data ranges I need, but I can not for the life of me figure out how to make it clear the same data from multiple sheets. Here is the code:
// This section adds the top menu
function onOpen() {
SpreadsheetApp.getUi().createMenu('Reset')
.addItem("Formulas Only", "ResetFormulas")
.addItem("Entire Page", "ResetPage")
.addToUi()
}
// This section makes a button to reset the whole page copies the entire page
function ResetPage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Data1'); // Enter tab name of what you’re copying from
sheet.getRange('A1:U39').copyTo(ss.getRange('A1:U39')) //enter entire tab range
}
// This section copies only the cells you specify
function ResetFormulas() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet12'); // Enter tab name here of what you’re copying
from
//Header
sheet.getRange('B6:E20').copyTo(ss.getRange('B6:E20')) //first range you’re copying
sheet.getRange('N6:Q20').copyTo(ss.getRange('N6:Q20')) //next range
sheet.getRange('B25:E39').copyTo(ss.getRange('B25:E39')) //etc
sheet.getRange('N25:Q39').copyTo(ss.getRange('N25:Q39')) //you get it
}
What specifically would I change to make it clear multiple sheets rather than the active one?
The line sheet.getRange('A1:U39').copyTo(ss.getRange('A1:U39')) specifies that data shall be copied to the range 'A1:U39' in the active spreadsheet
It doe not specify in which sheet the range is located, this is why Apps Script assumes that you want to copy into the active sheet
If you want to chose instead a different sheet - you need to specify the sheet
Sample:
function ResetPage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Data1'); // Enter tab name of what you’re copying from
var sheet2 = ss.getSheetByName('Data2'); // Enter tab name of what you’re copying from
sheet.getRange('A1:U39').copyTo(sheet2.getRange('A1:U39')) //enter entire tab range
}

Can I protect the name of a Google sheet? Or reference the sheet by position rather than 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.

Copy value from specific cell in google spreadsheet to another via button

On the click on a Button I want a function to be triggered that copies the value from Sheet1!A6 to Sheet2!A6. (see template) !
What do I have to add to this to make it work?
function CopyPasteA6() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange('A6').value
???
}
Thanks for help!
try like this, it's probably the simplest way to do it.
function CopyPasteA6() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
sheet1.getRange('A6').copyTo(sheet2.getRange('A6'))
}
then insert a drawing or an image in your sheet and assign the function to this image.
EDIT following your comment : this script copies to the next sheet if it exist. you'll have to put a button on each sheet though.... I'd suggest to use a menu instead that will be acessible from anywhere in the spreadsheet.
Note that 'A6' or "A6" are just the same ;-)
function CopyPasteA6() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheetidx = sheet.getIndex()-1 ;
var nextSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheetidx+1];
sheet.getRange('A6').copyTo(nextSheet.getRange('A6'))
}