Quando combinei as funções onEdit()
Agora me disseram que,
SyntaxError: O identificador 'sheet' já foi declarado linha: 27 arquivo: Code.gs
Por favor, deixe-me saber como eu crio identificadores exclusivos no seguinte 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"
}
Usando a seguinte planilha do Google: POC Scheduler
Sobre a mensagem de erro:
Isso acontece porque na linha 4, o script tem
Você pode remover a linha 27, mas na próxima vez que executar um script, terá um erro semelhante para a linha 28 atual, e assim por diante para qualquer declaração de variável
const
que use um nome usado anteriormente na declaração de variável.Provavelmente a maneira mais fácil de combinar dois gatilhos de edição é
onEdit
funções originais. Certifique-se de atribuir a cada uma delas um nome exclusivo.onEdit
função chamando as duas funções onEdit originais pelos seus novos nomes.Pegando o código de uma revisão anterior da sua pergunta, o código resultante deve ficar assim (você ainda deve verificar se ele funciona como esperado):
Relacionado