问题
I am trying to create a menu button in my Google Spreadsheet that runs a script. This script should extract specific rows and columns, then export them to an XML file.
I am trying to write the xml file using the ContentService, but it does not seem to work. Is there an example or tutorial that can help?
Thanks
回答1:
Are you getting any error messages? Or, what specifically is not working?
I've just tested it and it seems fine, here's my snippet
function doGet(e) {
var content;
try {
content = doIt(e);
} catch(err) {
content = '<error>' + (err.message || err) + '</error>';
}
return ContentService.createTextOutput(content)
.setMimeType(ContentService.MimeType.XML);
}
function doIt(e) {
if (!e) throw 'you cannot run/debug this directly\nyou have to either call the url or mock a call';
if (!e.parameter.id) throw '"id" parameter not informed. Please provide a spreadsheet id.';
var values = SpreadsheetApp.openById(e.parameter.id)
.getSheets()[0].getRange('A1:B2').getValues();
return '<sheet>' + values.map(function(row, i) {
return '<row>' + row.map(function(v) {
return '<cell>' + v + '</cell>';
}).join('') + '</row>';
}).join('') + '</sheet>';
}
By the way, I've published this script as a web-app running as the user for anyone to test: https://script.google.com/macros/s/AKfycbwrctRyspI2LnZ5hP8CMm7yY96jebLQS_4LShaKr_RIUKLm9qg/exec?id=paste-a-spreadsheet-id-here
来源:https://stackoverflow.com/questions/41377818/google-script-export-spreadsheet-to-xml-file