我有一个谷歌工作表,是更新从谷歌应用程序表-工作完美。是否有一个方法/脚本,我可以添加到谷歌工作表,将自动更新的,,一个excel表,谷歌驱动器或本地硬自动。(只有一个文件/覆盖)
任何有洞察力的人都很感激谢谢
发布于 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上的文件。
从你的问题上,我相信你的目标如下。
在本例中,下面的示例脚本如何?
示例脚本:
此示例脚本使用Drive。那么,请在高级谷歌服务上启用驱动器API.并且,请在Google上设置srcSpreadsheetId
和xlsxFileId
的变量。如果要使用活动电子表格,可以使用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
的电子表格覆盖。xlsxFileId
的XLSX文件不存在时,将创建一个新的XLSX文件。xlsxFileId
变量。这样,从下一次运行开始,输入ID的XLSX文件将被覆盖。参考资料:
发布于 2022-04-01 17:35:48
试一试
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); }
}
}
https://stackoverflow.com/questions/71710512
复制相似问题