Google App Script

时间:2017-02-12 05:30:11

标签: javascript google-sheets google-api

我得到了这个结果:

  

[[619,2016-12-06,03:08:04],[620,2016-12-06,03:08:18],[618,2016-12-06,03:09:40 ],[224,2016-12-06,03:29:14],[224,2016-12-06,03:41:55],[616,2016-12-06,06:44:59], [51,2016-12-06,07:05:21],[617,2016-12-06,07:12:50],[622,2016-12-06,07:29:26],[614 ,2016-12-06,07:31:01],[358,2016-12-06,07:39:52],[50,2016-12-06,07:41:43],[345,2016] -12-06,07:58:30],[479,2016-12-06,08:02:57],[374,2016-12-06,08:29:07],[594,2016-12 -06,12:18:40],[619,2016-12-06,16:17:33],[...]]

总共是272。

我尝试使用以下代码的脚本粘贴到Google表格中:

function addTimeToSheet(data){
  var ss = SpreadsheetApp.openById("11-_0QwEV_NGRY5nJUsOwyDxcmPGPXkLv636dQQ-SA5o");
  var sheets = ss.getSheetByName("TempKawit").getRange(1, 1).setValues([data]);

  return data;
}

表格中没有数据。我只想暂时将数据放在那里,如果再次运行脚本,它只会覆盖那里的内容。但是在运行脚本时,即使在第一次运行时没有表单中的任何数据,它也会出现如下错误:

Incorrect range width, was 272 but should be 1. 

但是如果我像这样编码函数:

function addTimeToSheet(data){
  Logger.log(data);
  var ss = SpreadsheetApp.openById("11-_0QwEV_NGRY5nJUsOwyDxcmPGPXkLv636dQQ-SA5o");
  var sheets = ss.getSheetByName("TempKawit");
  sheets.getRange(1, sheets.getLastColumn()+1,data.length,3).setValues([data]);

  return data;
}

我收到此错误:

Incorrect range height, was 1 but should be 272

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个

function addTimeToSheet(data){
  var ss = SpreadsheetApp.openById("11-_0QwEV_NGRY5nJUsOwyDxcmPGPXkLv636dQQ-SA5o");
  var noOfRow = data.length
  var noOfCol = data[0].length
  var sheets = ss.getSheetByName("TempKawit").getRange(1, 1,noOfRow,noOfCol).setValues(data);

  return data;
}

正确的语法: setValues(数据)不是: setValues([data])