I’m making a roster on Google spreadsheets, I usually use Excel so I’m new here.
I’m unable to test whether my script works because Google won’t let me run the code even though I’ve given it permissions to the file.
One thing I’d also like is to be able to duplicate the last duplicated roster if not only duplicating the first but updating the date to the current date a week in advance
here’s what I’m trying to do:
- duplicate a roster
- update all dates to add 7 days so it creates a new sheet on a trigger timer
- clear all past time/employee-hours slots in the file
I tried to run the script but I get this:
Warning
This project requires access to your Google Account to run. Please try again and allow it this time
can anyone help
// Google Apps Script
function duplicateSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Roster');
var newSheet = ss.duplicateSheet(templateSheet);
// Use a more descriptive name for the new sheet
var date = new Date();
newSheet.setName(`Roster - ${date.toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })}`);
// Move the updateDates and deleteTimeCells functions outside of duplicateSheet
// to make them reusable and easier to test
updateDates(newSheet);
deleteTimeCells(newSheet);
}
function updateDates(sheet) {
var range = sheet.getDataRange();
var values = range.getValues();
// Use a more efficient way to iterate over the values array
values.forEach(function(row) {
row.forEach(function(cell, j) {
if (cell instanceof Date) {
cell.setTime(cell.getTime() + 7 * 24 * 60 * 60 * 1000);
}
});
});
range.setValues(values);
}
function deleteTimeCells(sheet) {
var range = sheet.getDataRange();
var values = range.getValues();
// Use a more efficient way to iterate over the values array
values.forEach(function(row) {
row.forEach(function(cell, j) {
if (cell instanceof Date &&!isNaN(cell) && isTime(cell)) {
row[j] = '';
}
});
});
range.setValues(values);
}
function isTime(date) {
return date.getHours() > 0 || date.getMinutes() > 0 || date.getSeconds() > 0;
}
}}}}
Lorenzo Poe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.