2017-02-16 29 views

回答

1

這個腳本發送電子郵件與電子表格導出爲一個附件文件的Excel文件。 對於此腳本,需要輸入訪問令牌,工作表ID,Excel文件名和電子郵件地址。並且驅動器API在the API Console處爲「ON」。檢索訪問令牌的流程是https://developers.google.com/identity/protocols/OAuth2#scenarios

作用域是https://www.googleapis.com/auth/drivehttps://www.googleapis.com/auth/drive.file

function excelSender() { 
    var sheetID = [Sheet ID]; 
    var xlsxName = [Excel file name]; 
    var params = { 
    "headers" : {Authorization: "Bearer [Retrieved AccessToken]"}, 
    "muteHttpExceptions" : true 
    }; 
    var dUrl = "https://www.googleapis.com/drive/v3/files/" + sheetID + "/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
    var xlsxlFile = UrlFetchApp.fetch(dUrl, params).getBlob().setName(xlsxName); 
    MailApp.sendEmail({ 
    to: [Mail address], 
    subject: "sample subject", 
    body: "sample body", 
    attachments: [xlsxlFile] 
    }); 
} 

enter image description here

我能得到一個電子郵件與Excel文件。

+0

喜,此刻不起作用。我啓用了Drive API,圖片API和GMAIL API,但沒有奏效。發送電子郵件但附件沒有格式和擴展是不可能打開的。 –

+0

我可以問你關於你修改的錯誤和你的腳本嗎?有關個人信息,請填寫「#####」。當我運行這個,它工作正常。對於這個腳本,你不需要做一些Drive API和其他準備工作。當你運行這個時,會出現驗證對話框。當你按下對話框上的按鈕時,這個腳本就起作用了。請創建新的GAS項目並粘貼此腳本,然後重試。 – Tanaike

+0

你說現在爲你工作?不會出現錯誤,表單在電子郵件中附加顯示名稱而不會擴展。 –