Apps Script Trigger for IMPORTXML , OnChange / OnEdit [duplicate] - google-apps-script

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

Related

Google Sheets Script on Form sumbission [duplicate]

This question already has answers here:
Use a trigger to sort a sheet every time a form is submitted
(1 answer)
onEdit() function does not triggered when change was made by automatic script
(3 answers)
Closed last month.
I have a working script for Google sheets that checks 3 columns with trues and falses and writes baked TRUE/FALSE value for a 4th column when new data is entered.
function onEdit() {
var s = SpreadsheetApp.getActiveSheet(); if(s.getName() == "test" ) { //checks that we're on the right sheet
var r = s.getActiveCell();
if(r.getColumn() == 1) { //checks that the cell being edited is in column A
var trgtCell = r.offset(0, 7);
if(r.offset(0, 5) == "TRUE" && r.offset(0, 6) == "TRUE" && trgtCell.getValue() === ' ') //checks the rule + if the target (trgt) cell is empty
trgtCell.setValue("TRUE");
} else {
trgtCell.setValue("FALSE");
}}}
The sheet is connected with more Google Forms.
Now my script only works when data is manually entered but when the relevant form is submitted, it creates a new row and my script does not get active. I guess because it only triggers by "edit" which I guess is manually editing the sheets.
Can anyone help me how I could convert the script to work when form is submitted? That would be my main goal.

How to merge these two formulas to function onEdit [duplicate]

This question already has answers here:
Best Practices for Multiple OnEdit Functions
(1 answer)
Merging or Combining two onEdit trigger functions
(1 answer)
Closed 3 months ago.
I have two scripts that I am trying to run simultaneously that will organize two different columns based on their cell value. Both functions work, I'm just unsure how to combine the two
Could someone please help me?
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (r.columnStart != 9 || r.rowStart == 1 || e.value == src.getName()) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e.value);
src.getRange(r.rowStart,1,1,9).moveTo(dest.getRange(dest.getLastRow()+1,1,1,9));
src.deleteRow(r.rowStart);
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Opened");
var range = sheet.getRange("J2:J100");
function onEdit(e) {
range.sort([{column: 10, ascending: false}]);
}
I have tried combining both formulas however, I am aware that I cannot have two onEdit functions.

Event Trigger: Cannot read undefined property 'range' [duplicate]

This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 3 months ago.
Google Apps Script - triggered onEdit(e) with onEdit and oChange, both returned same error.
TypeError: Cannot read property 'range' of undefined code 6 ie; e.range
function onEdit(e) {
var range = e.range;
var spreadSheet = e.source;
var sheetName = spreadSheet.getActiveSheet().getName();
var row = range.getRow();
if(sheetName == 'DATA')
{
var new_date = new Date();
spreadSheet.getActiveSheet().getRange(row,6).setValue(new_date).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P");
}
}
`
Using SpreadSheet with 6 columns, the Col6 (F) for update (MM/DD/YYYY hh:mm:ss A/P) triggered with onedit. Tried necessary changes on colA to colE, but Col6 (F) not updating the for any changes.
Please note: for empty cell running the script, property return error due to "null" value is understood. But here even the changes made on SS does not return the UPDATE on col6(F). Kindly HELP
This is working for me:
function onEdit(e) {
e.source.toast('Entry')
const sh = e.range.getSheet();
if(sh.getName() == 'Sheet0') {
e.source.toast('Gate1')
var new_date = new Date();
sh.getRange(e.range.rowStart,6).setValue(new_date).setNumberFormat("MM/dd/yyyy hh:mm:ss A/P");
}
}
But you don't have any logic to control where you wish to monitor the ranges it just triggers on any edits to a given page.

onEdit function does nothing [duplicate]

This question already has answers here:
Google Sheets onEdit(e) TypeError: cannot read property
(2 answers)
Closed 2 years ago.
All the similar questions couldn't solve my issue.
I have a spreadsheet on google and I'm trying to write onEdit(e) function.
I've just typed a simple code to log some text and variables:
function onEdit(e){
Logger.log("Excuted");
var spreadsheet = e.source.getActiveSheet();
var activeCol = e.range.getColumn();
var activeRow = e.range.getRow();
Logger.log(activeRow + activeCol + spreadsheet);
}
Wherever I edit in the sheet nothing is logged.
I go to the editor and click run, the logger then shows this output:
Why the edit event is not triggered?
function onEdit(e){
const msg=Utilities.formatString('Row: %s, Col: %s SheetName: %s',e.range.rowStart, e.range.columnStart, e.range.getSheet().getName());
e.source.toast(msg);
}
Note: this function requires a onedit trigger event object so it cannot be run from menu or from the script editor. Just save it in the script editor and go edit some pages.

i need to run Google App script in onEdit [duplicate]

This question already has an answer here:
onEdit simple trigger never seems to be triggered
(1 answer)
Closed 3 years ago.
I'm trying to create a script so that when I edit text in column A then it automatically deletes text that is in column D of the same row and if column C in the same row (it contains ID of file from my drive) is not empty then delete that file.
I successfully created it and it's working when explicitly run the script in test , but the onEdit event never seems to get fired (not seeing log messages even).
anyone have an idea how it works ?
function onEdit(e) {
var currentSheet = e.range.getSheet();
var sheetEdited = currentSheet.getName();
var rowEdited = e.range.getRow();
var columnEdited = e.range.getColumn();
// if cell edited in sheet name form 1 and in column 1 then
if(sheetEdited == "form 1" && ( columnEdited == 1)){
// delete text in edited row column D
currentSheet.getRange(rowEdited,4).setValue('');
// if on edited row column C(that contain doc id ) if filled then detele that file
if (sheetId != "") {
var sheetId = currentSheet.getRange(rowEdited,3).getValues();
DriveApp.getFileById(sheetId).setTrashed(true);
}
}
}
The problem is that simple triggers can't perform operations that require authorization. So you have to use an installable trigger. Don't forget to change the name of the function.
function onEdit(e) {
var sh = e.range.getSheet();
if(sh.getName()=="form 1" && e.range.columnStart==1){
sh.getRange(e.range.rowStart,4).setValue('');//D
var id=sh.getRange(e.range.rowStart,3).getValue();//C
if(id!="")DriveApp.getFileById(id).setTrashed(true);
}
}