Issue:
Your code works fine for me.
According to the error message you are getting, you must be having some authorization issues and based on this and this make sure to include in the Manifest file the following Spreadsheet & Document scopes:
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/spreadsheets"]
and the appsscript.json
should look like that:
{
"timeZone": "Europe/Paris",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/spreadsheets"]
}
Although I post the recommended approach of generating and sending an excel file.
Recommended Approach:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const nameFile = ss.getName() + ".xlsx";
const requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
const url = "https://docs.google.com/spreadsheets/d/"+ ss.getId() + "/export?format=xlsx";
const result = UrlFetchApp.fetch(url , requestData);
const contents = result.getContent();
MailApp.sendEmail("Horgstar1212@gmail.com",
"Google Sheet to Excel",
"The XLSX file is attached",
{attachments:[{fileName:nameFile, content:contents, mimeType:"MICROSOFT_EXCEL"}]});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…