Export a Google Sheet to Google Drive in Excel format with Apps Script

老子叫甜甜 提交于 2019-12-01 13:47:37

问题


I would like to create a backup of my Google Spreadsheet into my Google Drive folder, but as an Excel file. I managed to create a code that create a copy of the gsheet and save it into the folder, but I could not change the code to save it as an Excel file.

Could you please help me with it?

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

回答1:


How about this answer? I think that there are several answers for your situation. So please think of this as one of them.

In order to convert spreadsheet to excel, the endpoint of https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx can be used. In this answer, this was used.

Modified script :

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

If I misunderstand your question, I'm sorry.



来源:https://stackoverflow.com/questions/49963584/export-a-google-sheet-to-google-drive-in-excel-format-with-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!