问题
I have found a script to export my google sheets workbook as .XLXS and email it to an address.
However it is not working when i try and set the name of the .xlxs file to a value in the workbook. code below:
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var name = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Project Details').getRange(2,A).getValue()
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( name + ".xlsx");
MailApp.sendEmail("(i know an email address goes here)", "Tablet Data Import", "The XLSX file is attached", {attachments: [blob]});
} catch (f) {
Logger.log(f.toString());
}
}
Some help would be appreciated
回答1:
The problem is caused by the way you use the method getRange()
There are two ways to do it correctly:
- Either use the getRange(a1Notation):
getRange('A2')
- Or use the getRange(row, column) notation:
getRange(2, 1)
回答2:
If the getValue change isn't effective then maybe...
Bring the setName function into the initial blob creation.
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName( name + ".xlsx");
to
var blob = UrlFetchApp.fetch(url, params).getBlob().setName( name + ".xlsx");
I'm not especially skilled in google apps script but I've found commands like this need to be after a var to be effective.
来源:https://stackoverflow.com/questions/60759727/exporting-google-sheet-as-xlxs-and-renaming