Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

api - Converting Google Sheet to Excel then sending as an email attachment works, but cannot access XLXS file (Unauthorized Error 401)

The code below works to convert and send a google sheets as an excel file over email, but the file does not have the actual excel, just Unauthorized Error 401.

function getGoogleSpreadsheetAsExcel(){

try { var ss = SpreadsheetApp.getActive();

var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&exportFormat=xlsx";

var params = {
  method      : "get",
  headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
  muteHttpExceptions: true
};

var blob = UrlFetchApp.fetch(url, params).getBlob();

blob.setName(ss.getName() + ".xlsx");

MailApp.sendEmail("Horgstar1212@gmail.com", "Google Sheet to Excel", "The XLSX file is  attached", {attachments: [blob]});}

catch (f) {
  Logger.log(f.toString()); }
}

I believe it has something to do with authorization to access the file, but I am very new to coding and API's, so any help would be appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"}]});     
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...