简体   繁体   English

Google App Script - 导出没有工作表名称的活动工作表

[英]Google App Script - Export Active Sheet Without Sheet Name

I am using the script below to export a sheet as Excel file but the output file name is always Document Name + Sheet Name ("Exported - Report.xlsx").我正在使用下面的脚本将工作表导出为 Excel 文件,但输出文件名始终是文档名称 + 工作表名称(“Exported - Report.xlsx”)。 How can I modify this to only use the sheet name as file name of the exported file ("Report.xlsx")?如何将其修改为仅使用工作表名称作为导出文件的文件名(“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");
}

In your situation, unfortunately, using the URL of ShtURL.toString().replace("/edit", "/export?format=xlsx&gid=" + ShtGID) , the filename cannot be directly changed.不幸的是,在您的情况下,使用ShtURL.toString().replace("/edit", "/export?format=xlsx&gid=" + ShtGID)的 URL,无法直接更改文件名。 So, in this case, how about the following modification?那么,在这种情况下,下面的修改呢?

Modified script:修改后的脚本:

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
}
  • In this modification, the converted XLSX is created as a temporal file.在此修改中,转换后的 XLSX 创建为临时文件。 Here, the filename is changed.在这里,文件名被更改。 And, the file is downloaded using the URL of the created file.并且,使用创建的文件的 URL 下载文件。 The temporal file is removed.临时文件被删除。

Note:笔记:

  • From your script, I thought that you might have wanted to use the sheet name of the 1st tab.从您的脚本中,我认为您可能想要使用第一个选项卡的工作表名称。 But, if you want to give the specific filename, please modify SheetApp.getSheets()[0].getSheetName() of blob.setName(SheetApp.getSheets()[0].getSheetName()) .但是,如果要给出具体的文件名,请修改SheetApp.getSheets()[0].getSheetName()blob.setName(SheetApp.getSheets()[0].getSheetName())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM