function UpdateCell()
{
const NotationRange = "J1";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const year = 2020;
sheets.forEach
(
(sheet) =>
{ const sheetName = sheet.getName();
{ sheet.getRange(NotationRange).setValue(year+'.'+sheetName);
}
}
)
}
in google spreadsheet script
upper code is work well and fast but, It change all of sheet
sheet name has rule. so, it can be distinguished
what I want is change cell value in specific sheet that sheet name contain [week]
in my sheet name are [Reference],[01.01],[01.02],,,,[12.31],[week01],[week02],,,[week53]
so I tried but fail..
function UpdateCell()
{
const NotationRange = "B2:D5"
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const year = 2020
const checkDailyWord = ".";
const checkWeekWord = "week";
var DC = new RegExp('\\b' + checkDailyWord + '\\b');
var WC = new RegExp('\\b' + checkWeekWord + '\\b');
sheets.forEach
(
(sheet) =>
{ const sheetName = sheet.getName();
if( DC.sheetName == 1 )
{ sheet.getRange(NotationRange).setValue(year+'.'+sheetName);
}
if( WC.sheetName == 1 )
{ sheet.getRange(NotationRange).setValue(year+'week'+sheetName);
}
}
)
}
new RegExp
has no property of sheetName
. Ref By this, undefined
is alwasy returned. I think that this might be the reason of your issue.includes
can be used for the if statement.When above points are reflected to your script, it becomes as follows.
function UpdateCell() {
const NotationRange = "B2:D5"
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const year = 2020
const checkDailyWord = ".";
const checkWeekWord = "week";
sheets.forEach(sheet => {
const sheetName = sheet.getName();
if (sheetName.includes(checkDailyWord) && !sheetName.includes(checkWeekWord)) {
sheet.getRange(NotationRange).setValue(year+'.'+sheetName);
} else if (!sheetName.includes(checkDailyWord) && sheetName.includes(checkWeekWord)) {
sheet.getRange(NotationRange).setValue(year+'week'+sheetName);
}
});
}
01.01,01.02,12.31
and week01,week02,week53
run sheet.getRange(NotationRange).setValue(year+'.'+sheetName)
and sheet.getRange(NotationRange).setValue(year+'week'+sheetName)
, respectively.week
and .
are not used.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.