0

我正在尝试使用 Google 脚本每小时从网站下载数据。代码如下所示。当我手动运行函数 CSV_sgj 时,我可以在电子邮件中收到所需的信息。但是当我将功能设置为时间驱动(每小时)时,我得到的都是#VALUE!。

我发现了一个类似的问题并试图改变

var sheet = SpreadsheetApp.getActiveSheet();

var sheet = SpreadsheetApp.openById("0AtAYfCLk3-h7dDBnckdSZkNXbkZBLXBHV200SGtuZnc");

但它仍然不起作用。

非常感谢您的帮助!

完整代码如下。

function readRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    Logger.log(row);
  }
};

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */

function CSV_sgj() {
  readRows();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("A1:N32");
  var data = range.getValues();

  var csv = "";
  for (var i = 0; i < data.length; ++i) {
    csv += data[i].join(",") + "\r\n";
  }
  var csvFiles = [{fileName:"PSI5.csv", content:csv}]
  MailApp.sendEmail("xxxxxx@gmail.com", "CSV", "", {attachments: csvFiles}); 
}
4

1 回答 1

0

当你使用

var sheet = SpreadsheetApp.openById("0AtAYfCLk3-h7dDBnckdSZkNXbkZBLXBHV200SGtuZnc");

返回的值是 aspreadsheet object而你想要的是 a sheet object... 所以你必须通过它的名称或索引号来获取工作表。我建议您相应地更改变量名称以保持清晰:

var ss= SpreadsheetApp.openById("0AtAYfCLk3-h7dDBnckdSZkNXbkZBLXBHV200SGtuZnc");
var sheet = ss.openByName('sheet1');// or any name you use (a string)
//or by its index
var sheet = ss.getSheets()[0] ;// 0 is the index of the first sheet in the spreadsheet (integer)
于 2013-05-22T05:49:52.300 回答