自动从 Google Sheet 导出到 Excel Sheet

Export automatically from a Google Sheet to an Excel Sheet

我有一个 google sheet,它是从 google 应用程序sheet 更新的 - 完美运行。 有没有我可以添加到 google sheet 的 method/script 会自动 update 一个 excel sheet google 驱动器或本地硬盘自动。 (只有一个 file/overwrite)

如有任何见解,我们将不胜感激 谢谢

尝试

function exportSheetAsXLSXToDrive() {
  var id=SpreadsheetApp.getActiveSpreadsheet().getId()
  var name=SpreadsheetApp.getActiveSpreadsheet().getName()
  var d = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm")
  let blob = getFileAsBlob("https://docs.google.com/spreadsheets/d/"+id+"/export?format=xlsx&portrait=false&exportFormat=xlsx");
  let file = DriveApp.createFile(blob).setName(name+'_'+d+'.xlsx');
  Logger.log(file.getUrl());
}
function getFileAsBlob(exportUrl) {
 let response = UrlFetchApp.fetch(exportUrl, {
     muteHttpExceptions: true,
     headers: {
       Authorization: 'Bearer ' +  ScriptApp.getOAuthToken(),
     },
   });
 return response.getBlob();
}

如果您只想保留最后一个文件(以前的文件可以在您的回收站中恢复:这可能是一个优势)

function exportSheetAsXLSXToDrive() {
  var id = SpreadsheetApp.getActiveSpreadsheet().getId()
  var name = SpreadsheetApp.getActiveSpreadsheet().getName() + '.xlsx'
  let blob = getFileAsBlob("https://docs.google.com/spreadsheets/d/" + id + "/export?format=xlsx&portrait=false&exportFormat=xlsx");
  delFile(name)
  let file = DriveApp.createFile(blob).setName(name);
}
function getFileAsBlob(exportUrl) {
  let response = UrlFetchApp.fetch(exportUrl, {
    muteHttpExceptions: true,
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken(), },
  });
  return response.getBlob();
}
function delFile(fileName) {
  var files = DriveApp.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    if (file.getName() == fileName) { file.setTrashed(true); }
  }
}

关于Is there a method/script that I can add to the google sheet that would automatically update an excel sheet on the google drive or local hard automatically.,现阶段使用GoogleApps Script,没有直接实现覆盖本地PC文件的方法。但是,对于 Google 驱动器上的文件,您可以覆盖该文件。

在此回答中,我建议使用 Google Apps 脚本覆盖 Google 驱动器上的文件。

从你的问题来看,我认为你的目标如下。

  • 您想通过使用 Google Apps 脚本将电子表格转换为 XLSX 数据来覆盖 Google 驱动器上现有的 XLSX 文件。

在这种情况下,下面的示例脚本怎么样?

示例脚本:

此示例脚本使用驱动器 API。所以,please enable Drive API at Advanced Google services。并且,请在 Google 驱动器上设置 srcSpreadsheetIdxlsxFileId 的变量。如果要使用活动电子表格,可以使用 const srcSpreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId().

function myFunction() {
  const srcSpreadsheetId = "###"; // Please set the source Spreadsheet ID.
  const xlsxFileId = "###"; // Please set the XLSX file ID.

  const url = "https://docs.google.com/spreadsheets/export?exportFormat=xlsx&id=" + srcSpreadsheetId;
  const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } });
  const blob = res.getBlob();
  if (Drive.Files.get(xlsxFileId).id) {
    const obj = Drive.Files.update({}, xlsxFileId, blob);
    console.log(obj.id); // Here, you can see that the XLSX file ID is not changed.
  } else {
    // If your "xlsxFileId" is not found, the converted XLSX data is created as a new XLSX file.
    const file = DriveApp.createFile(blob);
    console.log(file.getId()); // Please set this file ID to "xlsxFileId". By this, from the next run, the XLSX file of the inputted ID is overwritten.
  }
}
  • 当此脚本为运行时,当xlsxFileId的XLSX文件存在时,XLSX文件被srcSpreadsheetId的Spreadsheet覆盖。
  • 当此脚本为运行时,当xlsxFileId的XLSX文件不存在时,创建一个新的XLSX文件。
    • 在这种情况下,作为示例,将在 Google 驱动器上的根文件夹中创建一个新的 XLSX 文件。
    • 创建新的 XLSX 文件时,您可以在日志中看到文件 ID。请将此ID设置为xlsxFileId的变量。这样,从下一个运行开始,输入的ID的XLSX文件被覆盖

参考: