【问题标题】:Button that saves file with name specified in sheet保存工作表中指定名称的文件的按钮
【发布时间】: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


    【解决方案1】:

    先获取单元格的值:

    var filename = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("B97").getValue()+".xslx";
    
    folder.createFile(xlsxBlob.setName(filename));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-20
      • 2017-05-29
      • 1970-01-01
      • 2020-01-23
      • 2011-05-05
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多