I have a running script that updates the D:N column with current months. It does currently only update the ActiveSheet, I would like to refactor it so it runs on all 4 sheets in the spreadsheet.
Current Code:
function setDates(){
var monthNames = ["Jan (Qty)", "Feb (Qty)", "Mar (Qty)",
"Apr (Qty)", "May (Qty)", "Jun (Qty)",
"Jul (Qty)", "Aug (Qty)", "Sep (Qty)",
"Oct (Qty)", "Nov (Qty)", "Dec (Qty)"];
var currentMonths = SpreadsheetApp.getActiveSpreadsheet().getRange("D1:N1").getValues()[0];
var currentMonth = new Date().getMonth();
if (currentMonths[0] == monthNames[currentMonth]){return;}
for (var col = 0; col < currentMonths.length; col++){
SpreadsheetApp.getActiveSpreadsheet()
.getActiveSheet()
.getRange(1,4+col)
.setValue(monthNames[(currentMonth + col > 11 ?
currentMonth + col - 12 :
currentMonth + col)]);
}
}
What I've tried:
function sheetNamesFunction(){
var sheets = ['Sheet1',
'Sheet2',
'Sheet3',
'Sheet4'];
for (var s in sheets){
setDates(sheets[s]);
}
}
function setDates(sheetName){
var monthNames = ["Jan (Qty)", "Feb (Qty)", "Mar (Qty)",
"Apr (Qty)", "May (Qty)", "Jun (Qty)",
"Jul (Qty)", "Aug (Qty)", "Sep (Qty)",
"Oct (Qty)", "Nov (Qty)", "Dec (Qty)"];
var ss = SpreadsheetApp.openById('1Ed_x52cQx5A0RSqwxsB925wzbGt5kh0Gsi0ybl');
var sh = ss.getSheetByName(sheetName);
var currentMonths = sh.getRange("D1:N1").getValues()[0];
var currentMonth = new Date().getMonth();
if (currentMonths[0] == monthNames[currentMonth]){return;}
for (var col = 0; col < currentMonths.length; col++){
SpreadsheetApp.getActiveSpreadsheet()
.getActiveSheet()
.getRange(1,4+col)
.setValue(monthNames[(currentMonth + col > 11 ?
currentMonth + col - 12 :
currentMonth + col)]);
}
}
When I run this I get the following:
If I run log on
var sh = ss.getSheetByName(sheetName);
Logger.log(sh);