我如何添加一个新的选项卡,隐藏一个选项卡,改变一个公式,并在谷歌表格自动重命名它?



我有一个google表格文档,每个月的每一天都以日期作为表格名称。

例如,这个月我有10/1,10/2,10/3,10/4,10/5…等等,每天我复制旧的工作表,更新新工作表的标签名称,并隐藏最旧的工作表。

例如,如果我有标签10/1,10/2,10/3,10/4,10/5 ->有办法添加一个新的标签,命名为10/6(10/5)一样的内容和隐藏10/1吗?

我也有一个公式。例如,在标签10/6中,我将使用"=(B34+B30-'10/5'!B30)/B35"<——所有内容都在10/6以内,除了‘10/5’!B30,我需要在复制表格后手动更改。

所以当我创建10/7时,我需要将公式从'10/5'更改为'10/6'?

回顾一下:

  1. 我需要重复最近的标签和标签的名称更改为今天的日期。
  2. 我需要隐藏最古老的选项卡,所以在这个例子中我将创造10/6和隐藏10/1
  3. 我需要更改公式以匹配前一天,所以对于标签10/6,我需要公式链接到10/5

试过这个公式,但它一直标记为总天数,所以10/14 ->10/287

function duplicatesheet() {
var as = SpreadsheetApp.getActiveSpreadsheet(); // active spreadsheet
var s = as.getActiveSheet(); // first sheet object
var dateCell = "A1"; // cell containing first date
var N = 1; // number of copies to make
var startDate = new Date(s.getRange(dateCell).getValue()); // get the date stored in dateCell
var day = startDate.getDate(); // extract the day
var month = startDate.getMonth(); // extract the month
var year = startDate.getFullYear(); // extract the year
// loop over N times
for (var i = 0; i < N; i++) {
var asn = s.copyTo(as); // make a duplicate of the first sheet
var thisSheetDate = new Date(year, month, day+(i+1)); // store the new date as a variable temporarily
asn.getRange(dateCell).setValue(thisSheetDate); // writes the date in cell "B3"
asn.setName(Utilities.formatDate(thisSheetDate, "GMT", "MM/DD")); // sets the name of the new sheet
}
}

这将插入一个具有正确名称的新工作表,假设您仍然将工作表设置为10/10、10/11、10/12、10/13,那么该函数将确定下一个工作表的适当月份和日期。

function insertnewsheet() {
//find the sheets
const ss = SpreadsheetApp.getActive();
const shts = ss.getSheets().filter(sh => sh.getName().match(/d{1,2}/d{1,2}/g));
let oA = shts.map(sh => [sh.getName()]);
//Sort them based upon date value in milliseconds
oA.sort((a, b) => {
let tA = a[0].split('/');
let vA = new Date(new Date().getFullYear(), parseInt(tA[0]) -1 , parseInt(tA[1])).valueOf()
let tB = b[0].split('/');
let vB = new Date(new Date().getFullYear(),parseInt(tB[0]) - 1,  parseInt(tB[1])).valueOf();
return vA - vB;
});
Logger.log(oA.join('n'));
//take the last one and figure out the next month and day using the Date() object
let md = oA[oA.length - 1][0].split('/');
let dt = new Date(new Date().getFullYear(), parseInt(md[0]) - 1, parseInt(md[1]) + 1);
let name = `${dt.getMonth() + 1}/${dt.getDate()}`;
ss.insertSheet(name);
Logger.log(name);
}

相关内容

最新更新