GAS导出为CSV时的fileName参数

huangapple go评论59阅读模式
英文:

GAS fileName parameter when export as csv

问题

将表格下载为CSV文件。以下是我的工作代码:

function download_CSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssID = ss.getId();
  var sheetId = SpreadsheetApp.getActive().getSheetByName('Kitchen Doors Feed').getSheetId();

  var csv_format = "https://docs.google.com/spreadsheets/d/" + ssID + "/export" +
                                                                "?format=csv&" +
                                                                "=csv&" +
                                                                "gid=" + sheetId + "";
        
  Logger.log(csv_format);
  return [csv_format];
}

上述代码返回一个下载文件链接。该文件是以CSV格式命名的,与其引用的工作表标签相同(例如:Sheet1.csv)。

我想要自由地将文件命名为我喜欢的方式(例如:mytestfile.csv)。

为了实现这一点,我尝试在URL生成器(变量“csv_format”)中添加参数("title=filename"),如下所示,但是不起作用。

function download_CSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssID = ss.getId();
  var sheetId = SpreadsheetApp.getActive().getSheetByName('Kitchen Doors Feed').getSheetId();

  var csv_format = "https://docs.google.com/spreadsheets/d/" + ssID + "/export" +
                                                                "?format=csv&" +
                                                                "=csv&" +
                                                                "title=filename&" +
                                                                "gid=" + sheetId + "";
        
  Logger.log(csv_format);
  return [csv_format];
}

有帮助吗?

英文:

Download sheet as csv. Below is my working code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    function download_CSV() {
      var ss = SpreadsheetApp.getActiveSpreadsheet()
      var ssID = ss.getId();
      var sheetId = SpreadsheetApp.getActive().getSheetByName(&#39;Kitchen Doors Feed&#39;).getSheetId();

      var csv_format = &quot;https://docs.google.com/spreadsheets/d/&quot;+ssID+&quot;/export&quot;+
                                                            &quot;?format=csv&amp;&quot;+
                                                            &quot;=csv&amp;&quot;+
                                                            &quot;gid=&quot;+sheetId+&quot;&quot;;
    
      Logger.log(csv_format)
      return [csv_format];
    }

<!-- end snippet -->

The code above return a download file link. The file is CSV format named same as the Sheet tab to which it refers (example: Sheet1.csv).

I would like to be free to name the file as I pleased (example: mytestfile.csv).

To achieve that I tried to add the parameter ("title=filename") in the url builder (variable "csv_format") as showed below, but doesn't work.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function download_CSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var ssID = ss.getId();
  var sheetId = SpreadsheetApp.getActive().getSheetByName(&#39;Kitchen Doors Feed&#39;).getSheetId();
 

      var csv_format = &quot;https://docs.google.com/spreadsheets/d/&quot;+ssID+&quot;/export&quot;+
                                                            &quot;?format=csv&amp;&quot;+
                                                            &quot;=csv&amp;&quot;+
                                                            &quot;title=filename&amp;&quot;+
                                                            &quot;gid=&quot;+sheetId+&quot;&quot;;
    
      Logger.log(csv_format)
      return [csv_format];
    }

<!-- end snippet -->

Any help?

答案1

得分: 1

我相信这是不可能的。根据这篇其他帖子,参数 title 没有列出,因此无法使用,这就是当你尝试使用它时,当前 URL 忽略该参数的原因。

除此之外,如果你尝试在点击 Google Sheets 下载按钮时记录 HTTP 流量,你会看到请求 URL 只包括以下格式:

https://docs.google.com/spreadsheets/d/ID/export?format=csv&amp;id=ID&amp;gid=0

看起来默认行为是它将从文档本身获取名称,不会接受自定义名称。

总之,看起来这个参数无法用于 Google Docs,然而你可以通过创建 Google 问题追踪器 请求将其作为一个功能。

参考:

英文:

I believe this is not possible. According to this other post the parameter title is not listed, therefore it can not be used and that is the reason why the current URL ignores the parameter when you try to use it.

In addition to that, if you try to record the HTTP traffic when clicking the download button in Google Sheets you will see that the request URL only includes the following format:

https://docs.google.com/spreadsheets/d/ID/export?format=csv&amp;id=ID&amp;gid=0

It looks like the default behavior is that it will take the name from the document itself and would not accept custom names.

In conclusion, it looks like this parameter can not be used for Google Docs, however you could request this as a feature by creating a Google issue tracker.

Reference:

huangapple
  • 本文由 发表于 2023年4月4日 16:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定