on edit trigger for alertbox - google-apps-script

my onedit trigger is currently working with timestamp but when I try to change the purpose, instead of timestamp it will show an alertbox that's where my code won't work, anyone can help me with this?
var COLUMNTOCHECK = 2
var SHEETNAME = 'trial';
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var selectedCell = ss.getActiveCell();
var testRange = selectedCell.offset(0, 1);
var ui = SpreadsheetApp.getUi();
if(sheet.getSheetName() == SHEETNAME) {
if(selectedCell.getValue() == "test"){
ui.alert("popups",ui.ButtonSet.OK_Cancel);
}
}
}

Your button is incorrect. ui.ButtonSet.OK_Cancel is not a valid ButtonSet. It should be ui.ButtonSet.OK_CANCEL
Code:
var COLUMNTOCHECK = 2
var SHEETNAME = 'trial';
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var selectedCell = ss.getActiveCell();
var testRange = selectedCell.offset(0, 1);
var ui = SpreadsheetApp.getUi();
if(sheet.getSheetName() == SHEETNAME) {
if(selectedCell.getValue() == "test"){
ui.alert("popups",ui.ButtonSet.OK_CANCEL);
}
}
}
Output:

function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'trial' && e.range.columnStart == 2 && e.value == "test") {
SpreadsheetApp.getUi().alert("popups", ui.ButtonSet.OK_CANCEL);
}
}

Related

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

Modifying a flexible Google Sheets onEdit script to accommodate a wider range of inputs

Below is a script that allows me to combine five different onEdit functions into a single function.
function onEdit(e){
if (e.range.getA1Notation() != "H12" || e.value != "submitResponse") return;
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
var m = sh.getSheetByName("Master");
ss.getRange(2,1,ss.getLastRow(),3).copyTo(m.getRange(m.getLastRow()+1,1,ss.getLastRow(),3));
ss.getRange('H12').clearContent();
e.range.clearContents();
}
I'm now hoping to make this script more flexible. See Sheet1 of my spreadsheet: https://docs.google.com/spreadsheets/d/1EoOIQxWyKWOvtlCrmJNI76FAxGhzgXrE4s0F05tw2MY/edit#gid=0
It would be great if instead of limiting myself to H12, I could use the entire column of H:H to submit each corresponding row. So when I change H2 to submitResponse, it copies/pastes A2:G2 to the last row of the Master. When I change H3 to submitResponse, it copies/pastes A3:G3 to the last row of the Master. And so forth.
I tried my hand at this but no luck on execution. I guess I'm missing something.
​function onEdit(e) {
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
var actionCol = 8;
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
var colNumber = s.getLastColumn()-1;
if (e.value == "submitResponse" && colIndex == actionCol) ;
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
var targetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master").getRange(getLastRow()+1, 1, 1, colNumber);
sourceRange.copyTo(targetRange,SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
Try this:
function onEdit(e){
var sh=e.range.getSheet();
if (sh.getName()=='Sheet1' && e.range.columnStart==8 && e.range.rowStart>1 && e.value=="submitResponse") {
var msh=e.source.getSheetByName("Master");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,7).getDisplayValues()[0]);
}
}
function onEdit(e){
var sh=e.range.getSheet();
if (sh.getName()=='Sheet1' && e.range.columnStart==8 && e.range.rowStart>1 && e.value=="Yes") {
e.range.setValue('');
var msh=e.source.getSheetByName("Master");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,7).getDisplayValues()[0]);
}
}

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

About Google Sheets that is shared with users

I have a Google Sheets shared with others. My sheet name is "OrderList". I am looking for a script that grabs the user's email address and inputs into another cell when a user edits the sheet. I have written the following code, however it only returns the sheet owner's Gmail address:
var COLUMNTOCHECK1 = 5;
var COLUMNTOCHECK2 = 6;
var DATETIMELOCATION1 = [0,2];
var DATETIMELOCATION2 = [0,3];
var SHEETNAME = 'OrderList'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if( selectedCell.getColumn() == COLUMNTOCHECK1) {
var email = Session.getActiveUser().getEmail();
var dateTimeCell =
selectedCell.offset(DATETIMELOCATION1[0],DATETIMELOCATION1[1]);
dateTimeCell.setValue(new Date());
var dateTimeCell1 =
selectedCell.offset(DATETIMELOCATION2[0],DATETIMELOCATION2[1]);
dateTimeCell1.setValue(email);
}
if( selectedCell.getColumn() == COLUMNTOCHECK2) {
var dateTimeCell =
selectedCell.offset(DATETIMELOCATION2[0],DATETIMELOCATION2[1]);
dateTimeCell.setValue(new Date());
}
}
}
Since you're running this onEdit(e) you can use Event Objects, these are great for capturing the data you're trying to achieve. You'll need to set up an onEdit trigger for your project to capture the email address though. The code below should achieve what you're expecting.
var COLUMNTOCHECK1 = 5;
var COLUMNTOCHECK2 = 6;
var DATETIMELOCATION1 = [0,2];
var DATETIMELOCATION2 = [0,3];
var SHEETNAME = 'OrderList'
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
if( sheet.getSheetName() == SHEETNAME ) {
var selectedCell = ss.getActiveCell();
if( selectedCell.getColumn() == COLUMNTOCHECK1) {
var email = e.user.getEmail();
var dateTimeCell =
selectedCell.offset(DATETIMELOCATION1[0],DATETIMELOCATION1[1]);
dateTimeCell.setValue(new Date());
var dateTimeCell1 =
selectedCell.offset(DATETIMELOCATION2[0],DATETIMELOCATION2[1]);
dateTimeCell1.setValue(email);
}
if( selectedCell.getColumn() == COLUMNTOCHECK2) {
var dateTimeCell = selectedCell.offset(DATETIMELOCATION2[0],DATETIMELOCATION2[1]);
dateTimeCell.setValue(new Date());
}
}
}
As you can see, I've change your var email to use an event object to capture the email address of the user that is editing the spreadsheet:
var email = e.user.getEmail();

Make 'onEdit' work only on one of the sheets in the spreadsheet

I have an 'onEdit' script for a Google Sheets but only want it to work on one sheet:
function onEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var CellRow = SpreadsheetApp.getActiveRange().getRow();
var CellColumn = SpreadsheetApp.getActiveRange().getColumn();
if (CellColumn == 2 && CellRow == 3){
sheet.getRange(CellRow, CellColumn).setFormula( "=ArrayFormula(QUERY(ImportRange(\"0AmH7M5970cwZdHFRa3NITkdBSFhxd3JzYUZwbFRId3c\";\"Table!B2:J200\");\"select Col1 where Col1 = \'\"&$K$13&\"\' limit 1\"))");
Browser.msgBox("DON'T MESS WITH THE FORMULAS!");
}
}
How to I change the script so that it only works on one of the sheets in the worksheet?
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var CellRow = SpreadsheetApp.getActiveRange().getRow();
var CellColumn = SpreadsheetApp.getActiveRange().getColumn();
if (sheet.GetSheetName() == "MYSHEETNAME"){
if (CellColumn == 2 && CellRow == 3){
sheet.getRange(CellRow, CellColumn).setFormula( "=ArrayFormula(QUERY(ImportRange(\"0AmH7M5970cwZdHFRa3NITkdBSFhxd3JzYUZwbFRId3c\";\"Table!B2:J200\");\"select Col1 where Col1 = \'\"&$K$13&\"\' limit 1\"))");
Browser.msgBox("DON'T MESS WITH THE FORMULAS!");
}
}
}
Change MYSHEETNAME to the name of the sheet you want onedit to work for.