I have this sample Google Spreadsheet. I want to create a Google Apps Script function to export only the sheet named "TargetSheet" to Excel. Since TargetSheet might have formulas, it is important that this exportation has only values. Also, the colors and formats are important to be kept.
The final file should look like this.
Someone suggested me to use this snippet to export the sheet, but since I'm a begginner in Google Apps Script I didn't quite understand how to do that. Should I copy it to my code? Should I import it somehow? And after getting this function to work in my script, I assume I would need to create a function to put that to work. Would that look like this below?
function exportarPlanilha() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var file = exportSpreadsheetToFile_(spreadsheet, 'xlsx');
return file;
}
TargetSheet
) in the active Spreadsheet as a XLSX file.If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
When I saw the sample script of URL in your question, it seems that the flow is as follows.
In your case, the export type is the XLSX format which is the constant. So I think that the process cost can be reduced a little. So the modified script is as follows.
Please copy and paste the following script to the script editor of the Spreadsheet. And please set exportSheetName
. Then, please run the function exportarPlanilha
at the script editor. When the authorization screen is displayed, please authorize the scopes. By this, the script is run.
function exportarPlanilha() {
const exportSheetName = 'TargetSheet'; // Please set the target sheet name.
// 1. Copy the active Spreadsheet as a tempora Spreadsheet.
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet().copy('tmp');
// 2. Convert the formulas to the texts.
const targetRange = spreadsheet.getSheetByName(exportSheetName).getDataRange();
targetRange.copyTo(targetRange, {contentsOnly:true});
// 3. Delete the sheets except for a sheet you want to export.
spreadsheet.getSheets().forEach(sheet => {
if (exportSheetName != sheet.getName()) spreadsheet.deleteSheet(sheet)
});
// 4. Retrieve the blob from the export URL.
const id = spreadsheet.getId();
const xlsxBlob = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/export?id=${id}&exportFormat=xlsx`, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob();
// 5. Crete the blob as a file.
DriveApp.createFile(xlsxBlob.setName(`${exportSheetName}.xlsx`));
// 6. Delete the temporate Spreadsheet.
DriveApp.getFileById(id).setTrashed(true);
}
If I misunderstood your question and this was not the direction you want, I apologize.
Absolutely outstanding answer. Other two dots: (a) I would like to set in the function which is the folder link to store the xlsx and the file name, is that doable? If I'm going to create a button to run it, could I set those variables in the button? (b) Can I set the sheet to be exported to xlsx the active sheet instead of pulling it by the sheet name "TargetSheet"?
@abutremutante Thank you for replying. I'm glad your issue was resolved. About your additional questions, I could confirm that you have already posted it as new question. stackoverflow.com/q/60534241/7108653 and I could also confirm that it had already been resolved. I'm glad your new issue was resolved.