在Google工作表单元格中存储日期数组

时间:2017-09-02 15:14:06

标签: google-apps-script google-sheets

enter image description here

我正在尝试转换一系列天数:

#!/bin/bash
files_to_zip="test.png test2.png"
zipfile_name=result$(date "+%Y.%m.%d-%H.%M.%S").zip
zip "$zipfile_name"  "$files_to_zip"

到今天+1天的日期数组,今天+2等,然后将它们存储在工作表单元格中。在Calculating array of dates上成功,我正在使用

我有:

['1','2','3','4','5']。forEach(function(dayIncrement){       var date = new Date();       date.setDate(date.getDate()+ parseInt(dayIncrement));       dateRange.push(日期);     });

这是工作和生产:

[17-09-02 08:09:19:874 PDT] [Sun Sep 03 11:09:19 GMT-04:00 2017,Mon Sep 04 11:09:19 GMT-04:00 2017,Tue Sep 05 11:09:19 GMT-04:00 2017,Wed Sep 06 11:09:19 GMT-04:00 2017,Thu Sep 07 11:09:19 GMT-04:00 2017]

然而,当我尝试将数组插入单元格时,我会获得截图。如何从阵列中获取日期列表到单元格中,并且可以将数组存储在单元格中吗?

1 个答案:

答案 0 :(得分:2)

这将在PropertiesService中存储日期数组,并从PropertiesService中恢复它们,并向您显示它们与Utilities.formatDate()建立正确的日期。只需运行代码和对话框,这是合理的自我解释。

function arrayOfDays()
{
    var dayA=[1,2,3,4,5,6,7,8,9,10];
    var today=new Date().setHours(0,0,0,0);
    var day=24*60*60*1000;
    var days=[];
    for(var i=0;i<dayA.length;i++)
    {
      days.push(Utilities.formatDate(new Date(today + (i * day)), Session.getScriptTimeZone(), "MM/dd/yyyy"));

    }
    //Logger.log(days);
    var ui=HtmlService.createHtmlOutput('<h1>Array of Stored Dates</h2><br /><strong>This is the days array:<br/></strong> [' + days.join(', ') + ']');
    storeDates(days);
    var d=getDates();
    var s='<br /><strong>This is the array after it has been stored in PropertiesService and recovered from PropertiesService.</strong>';
    for(var i=0;i<d.length;i++)
    {
      s+='<br />' + Utilities.formatString('Date[%s]=%s <strong>Makes a new Date with Utilities.formatDate(new Date(d[i]), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss")</strong> -> %s', i,d[i],Utilities.formatDate(new Date(d[i]), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss"));
    }
    ui.append(s).setWidth(1200);
    SpreadsheetApp.getUi().showModelessDialog(ui, 'Array of Stored Dates')
}

function storeDates(dA)
{
  PropertiesService.getScriptProperties().setProperty('DateArray', dA.join(','));
}

function getDates()
{
  return PropertiesService.getScriptProperties().getProperty('DateArray').split(',');
}