将 Google 表格下载为 CSV 仅下载第一张表格

问题描述 投票:0回答:1

尝试以 CSV 格式下载 Google 表格,以保留时间戳的格式

我可以使用以下气体脚本来做到这一点。

但它只下载第一张。

我希望能够声明要下载的表格

如何做到这一点?

煤气

function autoDownload() {
  var html = HtmlService.createHtmlOutputFromFile('download');
  SpreadsheetApp.getUi().showModalDialog(html, 'CSV download dialog');
  Utilities.sleep(9000);
}

function saveAsCSV() {
  let ss     = SpreadsheetApp.getActiveSpreadsheet(),
      ssname = "sumApp-Headers",
      sheet  = ss.getSheetByName("Elements");

  var folder ="1KivXXXXfW3"  // DriveApp.getFileById(ss.getId()).getParents().next();
  var fileName = ssname + ".csv";
  var params = ss.getId() +  "/export?exportFormat=csv&format=csv";
  var url = "https://docs.google.com/spreadsheets/d/" + params;  
  var token = ScriptApp.getOAuthToken();  
  console.log(url)
  var response = UrlFetchApp.fetch(url + sheet.getSheetId(), {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });

  url = DriveApp.getFolderById(folder)
              .createFile(ssname, response, MimeType.CSV)
              .getDownloadUrl();
              //.replace("?e=download&gd=true","");         
    return url;
}

html

<!DOCTYPE html>
<html>
<body>
  Auto Download CSV ... please wait
</body>
<script>
  function executeDownload(url) {
      window.location.href = url;
  }
  window.onload=function() {
    google.script.run
      .withSuccessHandler(executeDownload)
      .saveAsCSV();
    window.setTimeout(function(){google.script.host.close()},9000);
  }
</script>
</html>
google-apps-script export-to-csv
1个回答
0
投票

关于

I want to be able to declare the sheet to download
,作为一个简单的修改,下面的修改怎么样?

来自:

var response = UrlFetchApp.fetch(url + sheet.getSheetId(), {

致:

var response = UrlFetchApp.fetch(url + "&gid=" + sheet.getSheetId(), {
  • 这样就使用了
    sheet  = ss.getSheetByName("Elements");
    给出的表格。
© www.soinside.com 2019 - 2024. All rights reserved.