Warm tip: This article is reproduced from stackoverflow.com, please click
google-apps-script google-sheets

Function to export only current sheet to Excel, with values and formats

发布于 2020-05-18 13:01:36

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;
}
Questioner
abutremutante
Viewed
34
Tanaike 2020-03-04 07:18
  • You want to export one of sheet (TargetSheet) in the active Spreadsheet as a XLSX file.
  • From the sample script of URL in your question, you want to create the XLSX file in your Google Drive.
  • You want to achieve this using the sample script of URL in your question with Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Flow:

When I saw the sample script of URL in your question, it seems that the flow is as follows.

  1. Copy the active Spreadsheet as a tempora Spreadsheet.
  2. Convert the formulas to the texts.
  3. Delete the sheets except for a sheet you want to export.
  4. Retrieve the export URLs.
  5. Retrieve the blob from the export URL.
  6. Crete the blob as a file.
  7. Delete the temporal Spreadsheet.

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.

Sample script:

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);
}
  • In this case, the temporal Spreadsheet is moved to the trash box.

Note:

  • In this case, please enable V8 at the script editor.
  • In above script, the XLSX file is created to the root folder. If you want to create the file to the specific folder, please tell me.

References:

If I misunderstood your question and this was not the direction you want, I apologize.