【发布时间】:2020-03-04 20:31:50
【问题描述】:
我有一张有按钮的工作表:
function exportarPlanilha() {
const exportSheetName = 'Demonstrativo'; // 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.
var folder = DriveApp.getFoldersByName("CLIENT_FOLDER").next();
folder.createFile(xlsxBlob.setName(`${exportSheetName}.xlsx`));
// DriveApp.createFile....
// 6. Delete the temporate Spreadsheet.
DriveApp.getFileById(id).setTrashed(true);
}
此脚本将工作表导出到我的 Google 云端硬盘中的文件夹(假设在脚本中指定为“CLIENT_FOLDER”)。但是,它将文件保存为“Demonstrativo.xlsx”('exportSheetName'),我需要它根据单元格 B97 中指定的名称保存文件(在本例中为“this_should_be_the_filename”,但实际上它是将许多变量连接成一个字符串的公式)。我怎么能这样做?
【问题讨论】:
标签: google-apps-script google-sheets