1

我想在我的 Google Drive 文件夹中创建我的 Google 电子表格的备份,但作为 Excel 文件。我设法创建了一个代码来创建 gsheet 的副本并将其保存到文件夹中,但我无法更改代码以将其保存为 Excel 文件。

你能帮我吗?

function makeCopy() {
  var formattedDate = Utilities.formatDate(new Date(), "CET", "yyyy-MM-dd' 'HH:mm");
  var name = "Backup Copy " + formattedDate;
  var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
  var file = DriveApp.getFileById("2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c")
  file.makeCopy(name, destination);
}
4

3 回答 3

6

这个答案怎么样?我认为您的情况有几个答案。因此,请将此视为其中之一。

为了将电子表格转换为 excel,https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx可以使用端点。在这个答案中,使用了这个。

修改后的脚本:

function makeCopy() {
  var formattedDate = Utilities.formatDate(new Date(), "CET", "yyyy-MM-dd' 'HH:mm");
  var name = "Backup Copy " + formattedDate;
  var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");

  // Added
  var sheetId = "2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c";
  var url = "https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx&access_token=" + ScriptApp.getOAuthToken();
  var blob = UrlFetchApp.fetch(url).getBlob().setName(name + ".xlsx"); // Modified
  destination.createFile(blob);
}

如果我误解了你的问题,我很抱歉。

更新日期:2020 年 2 月 7 日

从 2020 年 1 月起,访问令牌不能与access_token=###. 参考所以请使用请求标头的访问令牌而不是查询参数。如下。

var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
于 2018-04-22T07:49:25.003 回答
2

只是想将团队从 Tanaike 的答案中标记出来,并展示如何在通话期间将身份验证添加到标题中。

function convertSheetToXLSX() {
  var sheetId = "2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c";
  var spreadsheetName = "My Spreadsheet";
  var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
  var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + sheetId + "&exportFormat=xlsx";  
  var params = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true
  };
  var blob = UrlFetchApp.fetch(url, params).getBlob();
  blob.setName(spreadsheetName + ".xlsx");
  destination.createFile(blob);
}

于 2020-07-06T20:25:18.707 回答
0

我刚刚添加了一个简单的用户界面,使其作为日常工具更加有用。顶部脚本几乎是 MattMcCode 代码的直接副本。所以给他归因。

function exportSpreadsheetToXLSX(obj) {
  if (obj) {
    console.log(JSON.stringify(obj));
    var sheetId = obj.ssid;
    var spreadsheetName = obj.filename;
    var destination = DriveApp.getFolderById(obj.desid);
    var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + sheetId + "&exportFormat=xlsx";
    var params = {
      method: "get",
      headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() },
      muteHttpExceptions: true
    };
    var blob = UrlFetchApp.fetch(url, params).getBlob();
    blob.setName(spreadsheetName + ".xlsx");
    destination.createFile(blob);
  }
}

只需执行以下对话框即可输入您的 ssid、文件夹 ID 和文件名

function openSpreadsheetExportToXLSXDialog() {
  let html = '';
  html += '<html><head><style> input{margin: 2px 5px 2px 0;}</style></head><body>';
  html += '<form>';
  html += '<input type="text" name="ssid" size="50" placeholder="Source Spreadsheet Id" /><br />';
  html += '<input type="text" name="desid" size="50" placeholder="Destination Folder Id" /><br />';
  html += '<input type="text" name="filename" size="50" placeholder="Spreadsheet ID" /><br />';
  html += '<input type="button" value="Submit" onClick="processForm(this.parentNode)" />';
  html += '<input type="button" value="Exit" onClick="google.script.host.close();" />';
  html += '</form>';
  html += '<script>';
  html += 'function processForm(obj){console.log(obj);google.script.run.withSuccessHandler((obj)=>{google.script.host.close();}).exportSpreadsheetToXLSX(obj);}';
  html += '</script></body></html>';
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'Spreadsheet Export to XLSX Dialog');
}
于 2021-04-16T00:49:04.570 回答