I have a Spreadsheet with the following data's in each column, "A_email", "B_file_id", "C_Status", "D_Action" and "E_Result". I framed a script to add/remove editors (email - A column) on the google spreadsheets listed (File ID - B Column) based on the value in field "Status" (Enable/Disable - C Column). I can able to execute the desired function for all the rows (around 250) with the conventional script. But, I need to update only a particular row and so, i modified the script with onedit() trigger. The script fails on the line SpreadsheetApp.openById(). I came to know that i have to use onEdit() as Installable Triggers. As I am a beginner in Coding, Can anyone help me out to get the following corrected?
function onEdit(e) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var myRange = sheet.getRange("D2:D5");
var row = e.range.getRow();
var col = e.range.getColumn();
if (col >= myRange.getColumn() && col <= myRange.getLastColumn() && row >= myRange.getRow() && row <= myRange.getLastRow()) {
var file_id = sheet.getRange(row,2).getValue();
var status = sheet.getRange(row,3).getValue();
var action = sheet.getRange(row,4).getValue();
var email = sheet.getRange(row,1).getValue();
var file_1 = SpreadsheetApp.openById(file_id);
if (action == "Enable") {
const add_editor = [email];
file_1.addEditors(add_editor);
}
else if(action == "Disable"){
var editors = file_1.getEditors();
for (j = 0; j < editors.length; j++) {
file_1.removeEditor(editors[j]);
}
}
var update_range = sheet.getRange(row, 5).setValue("Updated");
}
}
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('onEdit')
.forSpreadsheet(ss)
.onEdit()
.create();
}
I can't able to grasp the concept through https://developers.google.com/apps-script/guides/triggers/installable#new-editor
onEdit
to others. Because the function name ofonEdit
is used for the simple trigger. So when the installable OnEdit trigger is installed to the function ofonEdit
, this function is run 2 times with the asynchronous processing. Ref So please be careful this. – Tanaike