使用Apps脚本

时间:2018-04-22 07:29:28

标签: google-apps-script google-sheets export-to-excel google-sheets-macros

我想将Google电子表格的备份创建到我的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);
}

3 个答案:

答案 0 :(得分:2)

这个答案怎么样?我认为你的情况有几个答案。所以请把它想象成其中之一。

为了将电子表格转换为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);
}

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

答案 1 :(得分:1)

只想把Tanaike的答案标记为team,并说明如何在通话过程中向头添加身份验证。

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);
}

答案 2 :(得分: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');
}