Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google Sheet API V4-Google应用程序脚本-Sheets.Spreadsheets.Values.update-追加日期_Google Apps Script_Google Sheets_Google Sheets Api - Fatal编程技术网

Google apps script Google Sheet API V4-Google应用程序脚本-Sheets.Spreadsheets.Values.update-追加日期

Google apps script Google Sheet API V4-Google应用程序脚本-Sheets.Spreadsheets.Values.update-追加日期,google-apps-script,google-sheets,google-sheets-api,Google Apps Script,Google Sheets,Google Sheets Api,我正在使用Sheets.Spreadsheets.Values.update将值粘贴到工作表中(由于使用range.setValues()时出现性能问题) 对于日期,源值是日期对象(在我使用range.setValues([[])时粘贴得很好) 现在的最终结果是使用字符串版本的日期单元格,例如,Mon Feb 28 00:00:00 GMT+01:00 2022,这些单元格与有效的gsheet日期不对应。我尝试了一些选项,但找不到实现此目标的方法。我相信您的目标如下 您希望使用带有Google

我正在使用Sheets.Spreadsheets.Values.update将值粘贴到工作表中(由于使用range.setValues()时出现性能问题)

对于日期,源值是日期对象(在我使用range.setValues([[])时粘贴得很好)


现在的最终结果是使用字符串版本的日期单元格,例如,Mon Feb 28 00:00:00 GMT+01:00 2022,这些单元格与有效的gsheet日期不对应。我尝试了一些选项,但找不到实现此目标的方法。

我相信您的目标如下

  • 您希望使用带有Google Apps脚本的Sheets API将值放入Google电子表格
  • 在当前版本中,值包括日期对象。当使用
    电子表格.values.update
    方法放置此日期对象时,该值不能用作日期对象。是否要解决此问题
为了实现您的目标,如何通过将日期对象转换为其他对象来放置值

模式1: 在此模式中,日期对象转换为序列号并放入电子表格

const spreadsheetId = "###";  // Please set the Spreadsheet ID.
const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.

// Here, the date values are converted to the serial number.
const convertedValues = values.map(r => r.map(c => c instanceof Date ? (c.getTime() / 1000 / 86400) + 25569 : c));

Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
const spreadsheetId = "###";  // Please set the Spreadsheet ID.
const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.

// Here, the date values are converted to the string value for parsing as the date object.
const convertedValues = values.map(r => r.map(c => c instanceof Date ? Utilities.formatDate(c, Session.getScriptTimeZone(), "yyyy/MM/dd HH:mm:ss") : c));

Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
  • 从unix时间转换为序列号的
    (c.getTime()/1000/86400)+25569
  • 在这个脚本中,日期对象被设置为序列号。因此,请将列“A”的数字格式设置为日期时间。这样,序列号就可以被视为日期时间
模式2: 在此模式中,日期对象转换为字符串值,作为日期对象进行解析,并放入电子表格

const spreadsheetId = "###";  // Please set the Spreadsheet ID.
const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.

// Here, the date values are converted to the serial number.
const convertedValues = values.map(r => r.map(c => c instanceof Date ? (c.getTime() / 1000 / 86400) + 25569 : c));

Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
const spreadsheetId = "###";  // Please set the Spreadsheet ID.
const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.

// Here, the date values are converted to the string value for parsing as the date object.
const convertedValues = values.map(r => r.map(c => c instanceof Date ? Utilities.formatDate(c, Session.getScriptTimeZone(), "yyyy/MM/dd HH:mm:ss") : c));

Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
  • 在这个脚本中,date对象作为字符串值,由
    用户输入
    作为date对象进行解析
  • 此示例使用
    yyyy/MM/dd HH:MM:ss
    作为日期格式。当无法解析此格式时,请根据您的情况修改此格式
参考资料:

    • 我相信你的目标如下

      • 您希望使用带有Google Apps脚本的Sheets API将值放入Google电子表格
      • 在当前版本中,值包括日期对象。当使用
        电子表格.values.update
        方法放置此日期对象时,该值不能用作日期对象。是否要解决此问题
      为了实现您的目标,如何通过将日期对象转换为其他对象来放置值

      模式1: 在此模式中,日期对象转换为序列号并放入电子表格

      const spreadsheetId = "###";  // Please set the Spreadsheet ID.
      const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.
      
      // Here, the date values are converted to the serial number.
      const convertedValues = values.map(r => r.map(c => c instanceof Date ? (c.getTime() / 1000 / 86400) + 25569 : c));
      
      Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
      
      const spreadsheetId = "###";  // Please set the Spreadsheet ID.
      const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.
      
      // Here, the date values are converted to the string value for parsing as the date object.
      const convertedValues = values.map(r => r.map(c => c instanceof Date ? Utilities.formatDate(c, Session.getScriptTimeZone(), "yyyy/MM/dd HH:mm:ss") : c));
      
      Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
      
      • 从unix时间转换为序列号的
        (c.getTime()/1000/86400)+25569
      • 在这个脚本中,日期对象被设置为序列号。因此,请将列“A”的数字格式设置为日期时间。这样,序列号就可以被视为日期时间
      模式2: 在此模式中,日期对象转换为字符串值,作为日期对象进行解析,并放入电子表格

      const spreadsheetId = "###";  // Please set the Spreadsheet ID.
      const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.
      
      // Here, the date values are converted to the serial number.
      const convertedValues = values.map(r => r.map(c => c instanceof Date ? (c.getTime() / 1000 / 86400) + 25569 : c));
      
      Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
      
      const spreadsheetId = "###";  // Please set the Spreadsheet ID.
      const values = [[new Date(), "b1", "c1"], [new Date(), "b2", "c2"]]; // This is a sample value for replicating your issue.
      
      // Here, the date values are converted to the string value for parsing as the date object.
      const convertedValues = values.map(r => r.map(c => c instanceof Date ? Utilities.formatDate(c, Session.getScriptTimeZone(), "yyyy/MM/dd HH:mm:ss") : c));
      
      Sheets.Spreadsheets.Values.update({values: convertedValues}, spreadsheetId, "Sheet1", {valueInputOption: "USER_ENTERED"});
      
      • 在这个脚本中,date对象作为字符串值,由
        用户输入
        作为date对象进行解析
      • 此示例使用
        yyyy/MM/dd HH:MM:ss
        作为日期格式。当无法解析此格式时,请根据您的情况修改此格式
      参考资料:

      谢谢@Tanaike的全面回复。我使用了模式1,它确实粘贴了日期(序列号)。但是,在我的情况下,我需要日期与用户时区保持不变。因此我添加了时区差异
      (c.getTime()-c.getTimeZoneOffset()*1000*60)/1000/86400+25569
      成功了。现在我还需要使用Sheets服务来处理异步问题,而不是setValues()@ajopor感谢您的回复。我很高兴您的问题得到了解决。谢谢@Tanaike的全面回复。我使用了模式1,它确实粘贴了日期(序列号)但是,在我的情况下,我需要日期与用户时区保持不变。因此,我添加了时区差
      (c.getTime()-c.getTimeZoneOffset()*1000*60)/1000/86400+25569
      ,并且它起了作用。现在我还需要使用Sheets服务而不是setValues()来处理异步问题@谢谢你的回复。我很高兴你的问题得到了解决。