Estoy tratando de agregar esta sección a mi script de aplicación de Google. Quiero agregar un script que cambie la celda para escribir, según el día de la semana. Por ejemplo:
If day=monday then write in cell S1 If day=tuesday then write in cell T1 If day=wednsday then write in cell U1etc.
¿Sabes cómo puedo lograr este resultado? ¡Gracias!
EDITAR: ¡Esto funciona para mí!
if (total != 'error') { if(new Date().getDay() === 0){ sh3.getRange('S'+counter).setValue(total); } else if(new Date().getDay() === 1){ sh3.getRange('T'+counter).setValue(total); } else if(new Date().getDay() === 2){ sh3.getRange('U'+counter).setValue(total); } else if(new Date().getDay() === 3){ sh3.getRange('V'+counter).setValue(total); } else if(new Date().getDay() === 4){ sh3.getRange('W'+counter).setValue(total); } else if(new Date().getDay() === 5){ sh3.getRange('X'+counter).setValue(total); } else if(new Date().getDay() === 6){ sh3.getRange('Y'+counter).setValue(total); } }En Javascript, puede obtener el día de la semana (índice numérico) usando el new Date().getDay() . La siguiente parte es obtener una referencia a la celda objetivo en la hoja de cálculo usando el método sheet.getRange() .
Recomendaría consultar los siguientes recursos para comprender cómo funciona la solución:
El siguiente código,
const spreadsheetId = "..."; const sheetName = "Sheet1"; const spreadsheet = SpreadsheetApp.openById(spreadsheetId); const sheet = spreadsheet.getSheetByName(sheetName); const today = new Date().getDay(); const cell = sheet.getRange(`S${today+1}`) cell.setValue("test");Si desea omitir el domingo y escribir en S1 el lunes, debe modificarlo
if (today > 0) { const cell = sheet.getRange(`S${today}`) cell.setValue("test"); } sheet.getRange(1, new Date().getDay() + 19).setValue()