首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自动从Google工作表导出到Excel工作表

自动从Google工作表导出到Excel工作表
EN

Stack Overflow用户
提问于 2022-04-01 17:05:24
回答 2查看 501关注 0票数 1

我有一个谷歌工作表,是更新从谷歌应用程序表-工作完美。是否有一个方法/脚本,我可以添加到谷歌工作表,将自动更新的,一个excel表,谷歌驱动器或本地硬自动。(只有一个文件/覆盖)

任何有洞察力的人都很感激谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-02 01:15:45

关于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.,在目前阶段,使用Google脚本,没有直接实现在本地PC上覆盖文件的方法。但是,对于Google上的文件,您可以覆盖该文件。

在这个答案中,我建议使用Google脚本覆盖Google上的文件。

从你的问题上,我相信你的目标如下。

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

在本例中,下面的示例脚本如何?

示例脚本:

此示例脚本使用Drive。那么,请在高级谷歌服务上启用驱动器API.并且,请在Google上设置srcSpreadsheetIdxlsxFileId的变量。如果要使用活动电子表格,可以使用const srcSpreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId()

代码语言:javascript
运行
复制
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的电子表格覆盖。
  • 当运行此脚本时,当xlsxFileId的XLSX文件不存在时,将创建一个新的XLSX文件。
    • 在本例中,作为示例,将创建一个新的XLSX文件到Google上的根文件夹。
    • 创建新的XLSX文件时,您可以在日志中看到文件ID。请将此ID设置为xlsxFileId变量。这样,从下一次运行开始,输入ID的XLSX文件将被覆盖。

参考资料:

票数 0
EN

Stack Overflow用户

发布于 2022-04-01 17:35:48

试一试

代码语言:javascript
运行
复制
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();
}

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

代码语言:javascript
运行
复制
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); }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71710512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档