2013-04-01 37 views
0

我試圖使用Google腳本每小時從網站下載數據。代碼如下所示。當我手動運行函數CSV_sgj時,我可以通過電子郵件收到所需的信息。但是,當我將函數設置爲時間驅動(每小時)時,我得到的都是#VALUE !.Google腳本中時間驅動的函數

我發現了一個類似的問題,並試圖改變

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("[email protected]", "CSV", "", {attachments: csvFiles}); 
} 

回答

0

當您使用

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

返回的值是一個spreadsheet object和你想要的是一個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) 
+0

嗨Serge insas!非常感謝您的幫助!有用! – user2230101

相關問題