Google 应用程序脚本 - 导出活动 Sheet 没有 Sheet 名称

Google App Script - Export Active Sheet Without Sheet Name

我正在使用下面的脚本将 sheet 导出为 Excel 文件,但输出文件名始终是文档名称 + Sheet 名称(“导出 - Report.xlsx”)。我如何修改它以仅使用 sheet 名称作为导出文件的文件名 ("Report.xlsx")?

function ExportSheet()
{
  var SheetApp = SpreadsheetApp.getActive(); 
  SheetApp.rename("Exported");
  
  ShtURL = SheetApp.getUrl();
  ShtID = SheetApp.getId();
  ShtGID = SheetApp.getSheetId();
  var url = ShtURL.toString().replace("/edit", "/export?format=xlsx&gid=" + ShtGID);
  
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );

  SheetApp.rename("Template");
}

不幸的是,在您的情况下,使用 ShtURL.toString().replace("/edit", "/export?format=xlsx&gid=" + ShtGID) 的 URL,无法直接更改文件名。那么,在这种情况下,下面的修改呢?

修改后的脚本:

function ExportSheet() {
  var SheetApp = SpreadsheetApp.getActive();
  SheetApp.rename("Exported");
  ShtURL = SheetApp.getUrl();
  ShtID = SheetApp.getId();
  ShtGID = SheetApp.getSheetId();
  var url = ShtURL.toString().replace("/edit", "/export?format=xlsx&gid=" + ShtGID);

  // --- I modified below script.
  var blob = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob();
  var file = DriveApp.createFile(blob.setName(SheetApp.getSheets()[0].getSheetName()));
  url = "https://drive.google.com/uc?export=download&id=" + file.getId();
  // ---

  var html = HtmlService.createHtmlOutput('<html><script>'
    + 'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
    + 'var a = document.createElement("a"); a.href="' + url + '"; a.target="_blank";'
    + 'if(document.createEvent){'
    + '  var event=document.createEvent("MouseEvents");'
    + '  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'
    + '  event.initEvent("click",true,true); a.dispatchEvent(event);'
    + '}else{ a.click() }'
    + 'close();'
    + '</script>'
    // Offer URL as clickable link in case above code fails.
    + '<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="' + url + '" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
    + '<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
    + '</html>')
    .setWidth(90).setHeight(1);
  SpreadsheetApp.getUi().showModalDialog(html, "Opening ...");
  SheetApp.rename("Template");

  file.setTrashed(true); // Added
}
  • 在此修改中,将转换后的 XLSX 创建为临时文件。这里,文件名被改变了。并且,使用创建文件的 URL 下载文件。临时文件已删除。

注:

  • 根据您的脚本,我认为您可能想使用第一个选项卡的 sheet 名称。但是,如果你想给出具体的文件名,请修改blob.setName(SheetApp.getSheets()[0].getSheetName())SheetApp.getSheets()[0].getSheetName()