【问题标题】:Exporting Google sheet as .XLXS and renaming将 Google 工作表导出为 .XLXS 并重命名
【发布时间】:2020-03-19 14:57:11
【问题描述】:

我找到了一个脚本,可以将我的 google 表格工作簿导出为 .XLXS 并将其通过电子邮件发送到一个地址。

但是,当我尝试将 .xlxs 文件的名称设置为工作簿中的值时,它不起作用。 代码如下:

function getGoogleSpreadsheetAsExcel(){

  try {

    var ss = SpreadsheetApp.getActive();

    var name = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Project Details').getRange(2,A).getValue()

    var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + ss.getId() + "&exportFormat=xlsx";

    var params = {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      muteHttpExceptions: true
    };

    var blob = UrlFetchApp.fetch(url, params).getBlob();

    blob.setName( name + ".xlsx");

    MailApp.sendEmail("(i know an email address goes here)", "Tablet Data Import", "The XLSX file is attached", {attachments: [blob]});

  } catch (f) {
    Logger.log(f.toString());
  }
} 

我们将不胜感激

【问题讨论】:

标签: google-apps-script


【解决方案1】:

问题是你使用方法getRange()的方式造成的

有两种正确的方法:

getRange('A2')

getRange(2, 1)

【讨论】:

    【解决方案2】:

    如果 getValue 更改无效,那么可能...

    将 setName 函数带入初始 blob 创建中。

    var blob = UrlFetchApp.fetch(url, params).getBlob();

    blob.setName(name + ".xlsx");

    var blob = UrlFetchApp.fetch(url, params).getBlob().setName(name + ".xlsx");

    我对谷歌应用脚​​本不是特别熟练,但我发现像这样的命令需要在 var 之后才能生效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多