目的: A1 中每张纸的日期 工作表名称为 1-31 表 '1'A1 获取手动输入的月初日期: 即2024年1月3日 工作表 '2'A1 为: ='1'!A1+SHEETNAME(0)-1
每次加载工作表或要求从中提取数据时,这些错误都会不断发生。
我的问题是:
注意:我每个电子表格有 31 张表格,每年 12 个电子表格, 且价值超过 2 年。我不想做超过 800 次编辑 手动。因此,解决方案不能是在每个设备上都有一个自定义编号。 床单。必须根据 SHEETNAME TIA
编写脚本我在使用当前代码的电子表格中遇到“同时运行的脚本太多”错误:
`/**
* Gets the Sheet Name of a selected Sheet.
*
* @param {number} option 0 - Current Sheet, 1 All Sheets, 2 Spreadsheet filename
* @return The input multiplied by 2.
* @customfunction
*/
function SHEETNAME(option) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet()
var thisSheet = sheet.getName();
//Current option Sheet Name
if(option === 0){
return thisSheet;
//All Sheet Names in Spreadsheet
}else if(option === 1){
var sheetList = [];
ss.getSheets().forEach(function(val){
sheetList.push(val.getName())
});
return sheetList;
//The Spreadsheet File Name
}else if(option === 2){
return ss.getName();
//Error
}else{
return "#N/A";
};
};`
通过常规脚本而不是多次调用自定义函数会更有效:
/** @OnlyCurrentDoc */
function setUpDates() {
const SS = SpreadsheetApp.getActiveSpreadsheet();
const baseDate = SS.getSheetByName("1").getRange("A1").getValue();
const sheets = SS.getSheets();
sheets.forEach(sheet => {
// Convert sheet names into numbers, all non-numbers will be NaN
const nameNum = sheet.getName() * 1;
// Skip if it is a wrong sheet or the first one with the base date
if (!nameNum || nameNum === 1) return;
sheet.getRange("A1").setValue(
// Add number of milliseconds in one day
new Date(baseDate.getTime() + 86400000 * (nameNum - 1))
);
})
}
// Optional: add to the main menu
function onOpen() {
var ui = SpreadsheetApp.getUi()
ui.createMenu("Setup")
.addItem("Set up dates", "setUpDates")
.addToUi()
}
备注: