2014-07-20 85 views
-2

我有一個Google腳本每天自動發送一封電子郵件。這是一個相當大的Google Spreadsheet,它具有多個「導入範圍」功能。我有一個完美的小電子表格。但是,在大型電子表格中,電子郵件發送時數據仍在加載。我相信這是因爲在下一個函數開始之前數據沒有時間加載。有誰知道腳本可以延遲腳本中的下一個功能嗎? 「SheetFlush」和「attachSendPDF」功能之間。該腳本的副本低於:需要腳本中的延遲功能

function SheetFlush(worksheet) { 
worksheet = worksheet || SpreadsheetApp.getActive(); 
var sheets = worksheet.getSheets(); 
SpreadsheetApp.flush(); 

}

function attachSendPDF() { 

    var ssID = SpreadsheetApp.getActiveSpreadsheet().getId(); 
    var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName(); 
    var email = ("[email protected]"); 
    var subject = "Master Update"; 
    var body = "Team, here is the Master Update."; 
    var todaysDate = new Date(); 
    var subjectDate = subject+" "+todaysDate 


//this is three level authorization 
    var oauthConfig = UrlFetchApp.addOAuthService("google"); 
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/"); 
    oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); 
    oauthConfig.setConsumerKey("anonymous"); 
    oauthConfig.setConsumerSecret("anonymous"); 

    //even better code 
    //oauthConfig.setConsumerKey(ScriptProperties.getProperty("consumerKey")); 
    //oauthConfig.setConsumerSecret(ScriptProperties.getProperty("consumerSecret")); 

    var requestData = { 
    "method": "GET", 
    "oAuthServiceName": "google", 
    "oAuthUseToken": "always" 
    }; 

    //"&gid=0&fit to width=true" part is for pdf to b in portrait mode and gid part exports only first sheet . u could have all sheets, so dont put any gid or the number of sheet u wish to export 
    var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" 
     + ssID + "&exportFormat=pdf&gridlines=true&printtitle=0&size=letter&fzr=true&portrait=0&fitw=true"; 

    var result = UrlFetchApp.fetch(url , requestData); 

    var contents = result.getContent(); 

    MailApp.sendEmail(email, subjectDate , body, {attachments:[{fileName:sheetName+todaysDate+".pdf", content:contents, mimeType:"application//pdf"}]}); 

} 
` 

謝謝!

回答

1

嘗試使用Utilities.sleep(毫秒)
請注意,它會消耗您的每日時間配額。