【问题标题】:Dynamic columns in one sheet based on another sheet values - Google Sheets一个工作表中基于另一个工作表值的动态列 - Google 表格
【发布时间】: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


    【解决方案1】:

    您找到了 80% 的解决方案,我只是对您的代码进行了一些更正以使其动态运行:

    function myFunction(){
    
      onOpen();
    }
    
    function onOpen() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var s1= ss.getSheetByName("Sheet1");
      var s2= ss.getSheetByName("Sheet2");
      var sDate = new Date(s1.getRange("B1").getValue()); // changed to B1 - the start date value, and sDate must be a Date Object, not string.
      var sDuration = s1.getRange("B3").getValue(); // changed to B3 - the duration value
      for (var cell = 0; cell < sDuration; cell++) {  // cell starts from 0
        var sMonth = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "MMM"); // I moved this into FOR loop
        var sYear = Utilities.formatDate(sDate, Session.getScriptTimeZone(), "YYYY"); // I moved this into FOR loop
        s2.getRange(1,cell+3).setValue(sMonth + ", " + sYear); // geting range with row & column value, not with Column name
        sDate.setMonth(sDate.getMonth()+1); // add one month to sDate
      }
    }
    

    在我更改的代码中有 cmets。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      稍微修改Change value of cell in same row with google script if column has certain value中的代码,就可以了。

      修改onOpen()为,

      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 lastColumn = s2.getLastColumn();
        for (var cell = 3; cell <= lastColumn + sDuration; cell++){
          var currentCell = s2.getRange(1, cell);
          currentCell.setValue(Utilities.formatDate(sDate, Session.getScriptTimeZone(), "MMM") + ", " 
                          + Utilities.formatDate(sDate, Session.getScriptTimeZone(), "YYYY"));
          sDate.setMonth((sDate.getMonth() + 1) % 12);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多