【发布时间】:2017-01-14 14:56:28
【问题描述】:
在 Google 电子表格中,Sheet1 有 3 个值。 [持续时间由 =DATEDIF(StartDate, EndDate, "M") 计算]
Sheet2 有 2 个预定义列(Col1、Col2)和需要根据 Duration 值附加的动态列。
Example 1:
StartDate = 01-Sep-2016 and EndDate = 30-Nov-2016
So the Duration is 2
Example 2:
StartDate = 01-Sep-2016 and EndDate = 31-Dec-2016
So the Duration is 3
由于列是动态的,是否可以根据列和行索引设置列值,而不是像下面的代码那样硬编码。
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1= ss.getSheetByName("Sheet1");
var s2= ss.getSheetByName("Sheet2");
var sDate = s1.getRange("B5").getValue();
var sDuration = s1.getRange("B7").getValue();
var sMonth = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "MMM");
var sYear = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "YYYY");
for (var cell = 1; cell <= sDuration; cell++) {
s2.getRange("C1").setValue(sMonth + ", " + sYear);
}
}
谢谢。
【问题讨论】:
标签: google-apps-script google-sheets