When I combined onEdit() functions
I'm now being told that,
SyntaxError: Identifier 'sheet' has already been declared line: 27 file: Code.gs
Please let me know how I create unique identifiers in the following Apps Script
function onEdit(e) {
onEditTimeValue(e);
const sheet = e.range.getSheet();
const sheetName = sheet.getName();
const sheetConfig = { // Configure the sheets and columns should be updated in "Scheduler".
"Sun": { targetColumn: 4 }, // Column D
"Mon": { targetColumn: 8 } // Column H
};
if (!(sheetName in sheetConfig)) return; // Only proceed if the edit is in a configured sheet.
const scheduleSheet = e.source.getSheetByName("Scheduler");
const range = e.range;
const column = range.getColumn();
const row = range.getRow();
if (row < 3 || (column !== 9 && column !== 13)) return; // Only proceed if the edit is in I3:I or M3:M.
const nameColumn = column + 1; // Determine the adjacent column.
const nameCell = sheet.getRange(row, nameColumn).getValue();
const updatedValue = range.getValue();
if (!nameCell) return; // Skip if no name is present
const scheduleNames = scheduleSheet.getRange("A3:A").getValues().flat(); // Get all names from Schedule!A3:A.
const scheduleRow = scheduleNames.indexOf(nameCell); // Find row number where the name exists in Schedule!A3:A.
if (scheduleRow === -1) return; // Skip if name not found
const targetColumn = sheetConfig[sheetName].targetColumn; // Determine target column (based on configuration).
scheduleSheet.getRange(scheduleRow + 3, targetColumn).setValue(updatedValue); // Update the corresponding column in "Schedule"
onEditHourValue(e);
const sheet = e.range.getSheet();
const sheetName = sheet.getName();
const sheetConfig = { // Configure the sheets and columns should be updated in "Scheduler".
"Sun": { targetColumn: 5 }, // Column E
"Mon": { targetColumn: 9 } // Column I
};
if (!(sheetName in sheetConfig)) return; // Only proceed if the edit is in a configured sheet.
const scheduleSheet = e.source.getSheetByName("Scheduler");
const range = e.range;
const column = range.getColumn();
const row = range.getRow();
if (row < 3 || (column !== 12 && column !== 16)) return; // Only proceed if the edit is in L3:L or P3:P.
const nameColumn = column - 2; // Determine the adjacent column.
const nameCell = sheet.getRange(row, nameColumn).getValue();
const updatedValue = range.getValue();
if (!nameCell) return; // Skip if no name is present
const scheduleNames = scheduleSheet.getRange("A3:A").getValues().flat(); // Get all names from Schedule!A3:A.
const scheduleRow = scheduleNames.indexOf(nameCell); // Find row number where the name exists in Schedule!A3:A.
if (scheduleRow === -1) return; // Skip if name not found
const targetColumn = sheetConfig[sheetName].targetColumn; // Determine target column (based on configuration).
scheduleSheet.getRange(scheduleRow + 3, targetColumn).setValue(updatedValue); // Update the corresponding column in "Schedule"
}
Using the following Google Sheet: POC Scheduler
Regarding the error message:
SyntaxError: Identifier 'sheet' has already been declared line: 27 file: Code.gs
This happens because in line 4, the script has
const sheet = e.range.getSheet();
You might remove line 27, but the next time you run a script, you will have a similar error for the current line 28, and so on for any variable declaration using const
that uses a name previously used on variable declaration.
Probably the easiest way to combine two on edit triggers is to
onEdit
functions. Please ensure that you assign each of them a unique name.onEdit
function calling the two original onEdit functions by their new names.Taking the code from a previous revision of your question, the resulting code should look like this (you still should review that it works as you expect):
function onEdit(e){
onEdit1(e);
onEdit2(e);
}
function onEdit1(e) {
const sheet = e.range.getSheet();
const sheetName = sheet.getName();
const sheetConfig = { // Configure the sheets and columns should be updated in "Scheduler".
"Sun": { targetColumn: 4 }, // Column D
"Mon": { targetColumn: 8 } // Column H
};
if (!(sheetName in sheetConfig)) return; // Only proceed if the edit is in a configured sheet.
const scheduleSheet = e.source.getSheetByName("Scheduler");
const range = e.range;
const column = range.getColumn();
const row = range.getRow();
if (row < 3 || (column !== 9 && column !== 13)) return; // Only proceed if the edit is in I3:I or M3:M.
const nameColumn = column + 1; // Determine the adjacent column.
const nameCell = sheet.getRange(row, nameColumn).getValue();
const updatedValue = range.getValue();
if (!nameCell) return; // Skip if no name is present
const scheduleNames = scheduleSheet.getRange("A3:A").getValues().flat(); // Get all names from Schedule!A3:A.
const scheduleRow = scheduleNames.indexOf(nameCell); // Find row number where the name exists in Schedule!A3:A.
if (scheduleRow === -1) return; // Skip if name not found
const targetColumn = sheetConfig[sheetName].targetColumn; // Determine target column (based on configuration).
scheduleSheet.getRange(scheduleRow + 3, targetColumn).setValue(updatedValue); // Update the corresponding column in "Schedule"
}
function onEdit2(e) {
const sheet = e.range.getSheet();
const sheetName = sheet.getName();
const sheetConfig = { // Configure the sheets and columns should be updated in "Scheduler".
"Sun": { targetColumn: 5 }, // Column E
"Mon": { targetColumn: 9 } // Column I
};
if (!(sheetName in sheetConfig)) return; // Only proceed if the edit is in a configured sheet.
const scheduleSheet = e.source.getSheetByName("Scheduler");
const range = e.range;
const column = range.getColumn();
const row = range.getRow();
if (row < 3 || (column !== 12 && column !== 16)) return; // Only proceed if the edit is in L3:L or P3:P.
const nameColumn = column - 2; // Determine the adjacent column.
const nameCell = sheet.getRange(row, nameColumn).getValue();
const updatedValue = range.getValue();
if (!nameCell) return; // Skip if no name is present
const scheduleNames = scheduleSheet.getRange("A3:A").getValues().flat(); // Get all names from Schedule!A3:A.
const scheduleRow = scheduleNames.indexOf(nameCell); // Find row number where the name exists in Schedule!A3:A.
if (scheduleRow === -1) return; // Skip if name not found
const targetColumn = sheetConfig[sheetName].targetColumn; // Determine target column (based on configuration).
scheduleSheet.getRange(scheduleRow + 3, targetColumn).setValue(updatedValue); // Update the corresponding column in "Schedule"
}
Related