Change script to work only on specific tab [duplicate] - google-apps-script

This question already has an answer here:
Google App Script onedit restrict to sheet
(1 answer)
Closed 8 months ago.
I have this script:
function onEdit2(e) {
const sheetName = "Charts"; // Please set your sheet name.
const dropDownCell = "B4:B6"; // Please set the range of dropdown list.
var spreadsheet = SpreadsheetApp.getActive();
var cell = spreadsheet.getActiveCell();
var cellR = cell.getRow();
var cellC = cell.getColumn();
var cellValue = cell.getValue();
if (cellR == 4 && cellC == 2) {
spreadsheet.getRange('B6').clearContent();
spreadsheet.getRange('B6').setValue('⚠️ SELECT');
}
}
I would like to make it work only on Charts tab without affecting any other tab. Right now script works in all tabs.

Actually the code doesn't make much sense to me. Here's what it boils down to:
function onEdit2(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Charts" && e.range.columnStart == 2 && e.range.rowStart == 4) {
sh.getRange('B6').setValue('⚠️ SELECT');
}
}

function onEdit2(e) {
const sheetName = "Charts"; // Please set your sheet name.
if( e.range.getSheet().getName() === sheetName ) {
const dropDownCell = "B4:B6"; // Please set the range of dropdown list.
var spreadsheet = SpreadsheetApp.getActive();
var cell = spreadsheet.getActiveCell();
var cellR = cell.getRow();
var cellC = cell.getColumn();
var cellValue = cell.getValue();
if (cellR == 4 && cellC == 2) {
spreadsheet.getRange('B6').clearContent();
spreadsheet.getRange('B6').setValue('⚠️ SELECT');
}
}
}

Related

Appending Array of data from one sheet to another

I have a google sheet with a column full of checkboxes. My goal is to have a row of the selected adjacent columns of data (namedInsured, lineCoverage, etc.) append to a separate sheet when a checkbox is selected and True. This is what I currently have, and it does absolutely nothing.
function onEdit(e) {
const ss = e.range.getSheet();
var active_range = ss.getActiveRange();
var namedInsured = ss.getRange(active_range.getRowIndex(), 4).getValue();
var lineCoverage = ss.getRange(active_range.getRowIndex(), 5).getValue();
var effDate = ss.getRange(active_range.getRowIndex(), 6).getValue();
var assignedTo = ss.getRange(active_range.getRowIndex(), 15).getValue();
var data = [namedInsured, lineCoverage, effDate, assignedTo];
const destinationSheet = ss.getSheetByName("Sheet4");
if (ss.getName() != "Sheet4" && e.range.columnStart == 10 && e.value == "TRUE")
{
e.source.toast("Quote In Process Entered")
destinationSheet.appendRow(data);
}
}
[This is an example of the desired output]
<< If the first and fourth checkboxes were selected one after the other, this is how they should appear on the separate sheet
This works for me
function onEdit(e) {
e.source.toast('Entry')
const sh = e.range.getSheet();
if (sh.getName() == "Sheet0" && e.range.columnStart == 10 && e.value == "TRUE") {
e.source.toast('Flag1');
const [a,b,c] = sh.getRange(e.range.rowStart, 4,1,3).getValues()[0];
var d = sh.getRange(e.range.rowStart, 15).getValue();
var data = [a, b, c, d];
const dsh = e.source.getSheetByName("Sheet1");//modified from original
dsh.appendRow(data);
}
}
Demo:

In Google Apps Script, debug tool says e is undefined? Script doesn't work when editing in sheet [duplicate]

This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 1 year ago.
The function in question:
function onEdit(e) {
var range = e.range;
var spreadSheet = e.source;
var sheetName = spreadSheet.getActiveSheet().getName();
var column = range.getColumn();
var inputValue = e.value;
var sheet = e.source.getActiveSheet(); if (sheet.getName() !== 'By Type' && sheet.getName() !== 'By State') {return;}
if(sheetName == 'By State' && column == 7)
{
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('By Type').getRange(range).setValue([inputValue]);
}
if(sheetName == 'By Type' && column == 7)
{
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('By State').getRange(range).setValue([inputValue]);
}
}
isn't e already defined? What do I need to change?
I added the font change so that I could see the script doing something.
function onEdit(e) {
//e.source.toast('Entry')
const sh = e.range.getSheet();
if(sh.getName() == 'Sheet0' && e.range.columnStart == 7) {
//e.source.toast('flag1');
e.range.setValue(e.value).setFontWeight('bold');
}
}
Tested and Functioning Properly

how to use a project on more than one sheet

I have the code below in my project, that helps me autofill the timestamp in "datetime" column in "test" sheet.
I want it to work for other sheets too. But I couldn't get it to work. Any help?
var SHEET_NAME = 'test';
var DATETIME_HEADER = 'datetime';
function getDatetimeCol(){
var headers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).getDataRange().getValues().shift();
var colindex = headers.indexOf(DATETIME_HEADER);
return colindex+1;
}
function onEdit() {
var ss = SpreadsheetApp.getActiveSheet();
var cell = ss.getActiveCell();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol());
if (ss.getName() == SHEET_NAME && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("yyyy-MM-dd hh:mm");
}
};
If you want to have an onEdit() trigger runs on multiple sheets use installable trigger.
Take your code and put it in a standalone script, not bounded to a sheet.
Then create function to setup onOpen Trigger
/**
* Creates a trigger for when a spreadsheet opens.
*/
function createSpreadsheetOnOpenTrigger() {
var id = 'YOUR_SHEET_ID';
var ss = SpreadsheetApp.openById(id);
ScriptApp.newTrigger('NAME_OF_FUNCTION_TO_RUN')
.forSpreadsheet(ss)
.onOpen()
.create();
}
reference : link
Then you just have to change id to setup trigger for all sheets you want the code run.
In the code take care to change to get infomration regarding celle and sheet from the event object :
function functionToRunOnEdit() {
var sheet = **e.range.getSheet()**;
var cell = **e.range**;
var name = sheet.getName();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol(sheet));
if (SHEET_NAMES.includes(name) && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("yyyy-MM-dd hh:mm");
}
};
Reference : link
I have also changed the variable name ss into sheet becausse it is a sheet but not a spreadsheet.
var SHEET_NAMES = ['test', 'test2'];
var DATETIME_HEADER = 'datetime';
function getDatetimeCol(sheet){
var headers = sheet.getDataRange().getValues().shift();
var colindex = headers.indexOf(DATETIME_HEADER);
return colindex+1;
}
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
var name = sheet.getName();
var datecell = ss.getRange(cell.getRowIndex(), getDatetimeCol(sheet));
if (SHEET_NAMES.includes(name) && cell.getColumn() == 1 && !cell.isBlank() && datecell.isBlank()) {
datecell.setValue(new Date()).setNumberFormat("yyyy-MM-dd hh:mm");
}
};

Add Checkbox with onEdit, as new row added to Spreadsheet - Apps Script

I have the below script that adds a timestamp when a new row of data is added.
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if(s.getName() !== 'Sheet7' || e.range.columnStart != 1 || e.range.rowStart < 1) return;
e.range.offset(0, 1).setValue(e.value ? new Date() : "");
}
I tried the below for adding the checkbox, doesnt work.
function AddCheckBox() {
var cell = SpreadsheetApp.getActive().getDataRange('A1:B');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
}
I would like it to also add a checkbox as well to the column next to where the timestamp appears.
Thanks in Advance
I just worked it out if anyones interested
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if(s.getName() !== 'TEST' || e.range.columnStart != 1 || e.range.rowStart < 1)
return;
e.range.offset(0, 1).setValue(e.value ? new Date() : "");
var spreadsheet = SpreadsheetApp.getActive();
var cell = SpreadsheetApp.getActive().getRange('C1');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation()
.requireCheckbox()
.build();
cell.setDataValidation(rule);
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('C1').activate();
spreadsheet.getCurrentCell()
.getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getActiveRange()
.autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
spreadsheet.getRange('A1').activate();
}

Copy down Formula in Google Sheets when cell is edited with any number or text

So I have been working on a script to copy a formula down when a cell is edited to Yes in the column next to. This is working.
My question is I'd like to copy down the formula if there's a text or number in that a cell.
How can I add this my script.
I am fairly knew to this so any advice or documentation would be appreciated.
function onEdit(){
var sheetNameToWatch = "Request for Purchases";
var columnNumberToWatch = 10;
var valueToWatch="Yes";
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getRange("K2").setFormula("=(c2*d2)");
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var val = sheet.getActiveCell().getValue();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && val==valueToWatch ) {
var targetCell = sheet.getRange(range.getRow(), range.getColumn()+1);
var d = ss.getRange("K2").copyTo(targetCell);
}
}
Is this what your trying to do?
function onEdit(e){
var sh=e.range.getSheet();
if(sh.getName()!='Request for Purchases') return;
if(e.range.columnStart==10 && e.value="Yes" ) {
e.range.offset(0,1).setFormula("=(C2*D2)");
}
}