This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to move a row from one spreadsheet (JobRequest) to another (Accepted) spreadsheet by clicking the checkbox automatically (no yes/no button) (located in col G). There are 7 columns of data to be moved. I want a dialog to popup at the time the box is checked too, to confirm that they have read that dialog box and accepted.
Ideally, I can log this and if they exit out of the dialog box the box unchecks, but I haven't gotten that far yet, so that's for another post.
For now, can you help be figure out where I went wrong for this checkbox to move to another sheet?
I keep getting the error
Error
TypeError: Cannot read property 'source' of undefined
I thought I already had it defined.
function onEdit(e){
const src = e.source.getActiveSpreadsheet();
const r = e.range;
if(src.getName()!= 'JobRequests' || r.columnStart !=7 || r.rowStart == 1) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Accepted');
src.getRange(r.rowStart,1,1,7).moveTo(dest.getRange(dest.getLastRow()+1,1,1,7));
src.deleteRow(r.rowStart);
}
function showFeedbackDialog() {
var widget = HtmlService.createHtmlOutputFromFile("AcceptanceForm.html");
widget.setWidth(400);
widget.setHeight(500);
SpreadsheetApp.getUi().showModalDialog(widget, "Send feedback");
}
Modification points:
When I saw your question and script, the reason of your issue might be due to getActiveSpreadsheet() of const src = e.source.getActiveSpreadsheet();. e.souece returns Spreadsheet object. In this case, getActiveSpreadsheet() cannot be used. Please use getActiveSheet().
In order to open a dialog, in this modification, I used SpreadsheetApp.getUi().alert().
In order to record a log, I used other sheet.
In order to uncheck the checkbox, uncheck() is used.
When these points are reflected in your script, how about the following modification?
Modified script:
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (src.getName() != 'JobRequests' || r.columnStart != 7 || r.rowStart == 1 || !r.isChecked()) return;
const ui = SpreadsheetApp.getUi();
const res = ui.alert("Move row?", ui.ButtonSet.YES_NO);
if (res == ui.Button.NO) {
r.uncheck();
return;
}
const dest = e.source.getSheetByName('Accepted');
src.getRange(r.rowStart, 1, 1, 7).moveTo(dest.getRange(dest.getLastRow() + 1, 1, 1, 7));
src.deleteRow(r.rowStart);
r.uncheck();
const log = e.source.getSheetByName("log") || e.source.insertSheet("log");
log.appendRow([new Date(), `row ${r.rowStart} was moved.`]);
}
When you use this script, please check the checkbox in column "G" of "JobRequests" sheet. By this, the script is run and you can see a dialog.
And, in this modification, a log is recorded to "log" sheet. But, in this case, please modify the log data in the above script for your actual situation.
References:
Event Objects
getActiveSheet()
uncheck()
This question already has answers here:
onChange Trigger not working as expected
(1 answer)
Timestamp Google Script triggered by formula
(2 answers)
Closed 7 months ago.
I am currently in the middle of making a project, whereas data is being pulled from website via IMPORTXML. I tried editing and searching for an answer to the question: how do I get a Timestamp in Column "AP" when there is data changed to Column "N" ? I've tried simple & installable triggers, but none seem to work.
Link to the file (Spreadsheets)
I have never really had to use Apps Script for Google Spreadsheet, so I am sorry if there's some dumb coding there.
function onChange2(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
if(col == 14){
e.source.getActiveSheet().getRange(row,42).setValue(new Date());
}
}
Didn't work
function createSpreadsheetChangeTrigger() {
var ss = SpreadsheetApp.getActive();
var row = range.getRow();
var col = range.getColumn();
ScriptApp.newTrigger('onChange2')
.forSpreadsheet(ss)
.onChange()
.create();
if(col == 14){
source.getActiveSheet().getRange(row,42).setValue(new Date());
}
}
> No Luck here too
This question already has answers here:
How to fix 'TypeError: Cannot call method "getRange" of null' in Google Apps Script
(1 answer)
Google Apps Script, copy one spreadsheet to another spreadsheet with formatting
(5 answers)
Closed 1 year ago.
The goal of this function is to copy a new google sheet from the source spreadsheet into a source target spreadsheet.
The funcion insertNewSheet () is a minimal reproducible example. In this example for semplicity it run only if there is no the same sheet in target spreadsheet.
The problem arises when coping the cell values from source sheet to target sheet: in fact I receive always the same error: "TypeError: Cannot read property 'getRange' of null". I checked the range (Logging log) and really the range is not null. So I am struggling with this problem for many hours and I have not the solution.
function insertNewSheet() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = spreadSheet.getSheetByName('Query_Bando');
var nameOfCell= spreadSheet.getSheetByName('Query_Bando').getRange('A2').getValue();
var sourceValues=sourceSheet.getRange("A13").getDataRegion().getValues();
var cellDataRegion=sourceSheet.getRange("A13").getDataRegion();
var lastRowSource= cellDataRegion.getLastRow();
var lastColumnSource= cellDataRegion.getLastColumn();
var tss=SpreadsheetApp.openById("1bh563zHW0WMIcalWP2t3zG1_sK319JuMZwPsWhucKAs");
var ts=tss.getSheetByName(nameOfCell);
//get A1 notation identifying the range
var A1Range = cellDataRegion.getA1Notation();
var SRange = sourceSheet.getRange(A1Range);
tss.insertSheet(nameOfCell);
var SData = SRange.getValues();
//Logger.log(sourceValues);
Logger.log(lastRowSource);
Logger.log(lastColumnSource);
//Logger.log(SRange);
Logger.log(SData);
Logger.log(A1Range);
ts.getRange(A1Range).activate().setValues(SData);
}
This question already has an answer here:
MailApp.sendEmail Error Message - "do not have permission to call sendEmail" [duplicate]
(1 answer)
Closed 2 years ago.
I'm attempting to access data from one Google Sheets spreadsheet in order to update entries in another Google Sheets spreadsheet.
The code I've written to accomplish this so far is as follows:
var ss1 = SpreadsheetApp.openById("123456789").getSheetByName('Sheet1');
function onEdit (e) {
var sheet = e.source.getActiveSheet();
sheet.getRange("C4").setValue(ss1.getUrl());
}
But I'm getting the following error when I edit an entry in the target spreadsheet to trigger the script:
Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets
at unknown function
How do I obtain permissions to run the openByID() method or is there a better way to access an external Google Sheets spreadsheet's data?
/*
* The trigger function to call
*/
function updateOtherSheet(e) {
var ss1 = SpreadsheetApp
.openById("123456789")
.getSheetByName('Sheet1');
var sheet = e.source.getActiveSheet();
sheet.getRange("C4").setValue(ss1.getUrl());
}
/*
* Install the trigger by script `Run once`
* or use the edit menu 'current project triggers'
*/
function createTrigger() {
ScriptApp
.newTrigger('updateOtherSheet')
.forSpreadsheet(ss) // <- spreadsheet object var goes here !not string
.onEdit()
.create()
}
This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 4 years ago.
I wanted to execute a function whenever a cell is edited or added in google spreadsheet.
I have the below function, but it is not working. please help. Also I wanted to call the function Start sync after X seconds. How to give a delay in calling the startSync()
function onEdit(e) {
var range = e.range;
range.setNote('Last modified: ' + new Date());
startSync()
}
whenever I edit a cell , a note (comment) is getting added to the cell , but the startSync is not getting called.
function startSync() {
//Get the currently active sheet
var sheet = SpreadsheetApp.getActiveSheet();
//Get the number of rows and columns which contain some content
var [rows, columns] = [sheet.getLastRow(), sheet.getLastColumn()];
//Get the data contained in those rows and columns as a 2 dimensional array
var data = sheet.getRange(1, 1, rows, columns).getValues();
syncMasterSheet(data);
}
Answer:
Installable Triggers helped me to call StartSync()
You can't use a simple onEdittrigger with most .set methods. You need to use an installed onEdit() trigger instead. See this answer for more information on triggers
To delay the start of the function use Utilities.sleep(milliseconds) before the call to the startSync() function. See more info about the Utilities class here