Google apps script 我可以用应用程序脚本覆盖保存在Google Drive上的Excel文件(从Google工作表导出)的内容吗
我使用Google Apps脚本将Google工作表作为Excel文件导出到Google Drive。然后,Excel文件通过Google Drive桌面应用程序与其他6名用户同步到其本地计算机。每次导出Google工作表时,它都会创建一个新文件,而不是替换旧文件(尽管文件名相同),并删除原始版本。这很好,只是当它与Google Drive Desktop同步时,Windows会删除原始版本并将其发送到回收站 该文件每15分钟创建一次,大小为3mb,因此几周后回收站就会充满千兆字节的数据 是否可以更新文件内容而不是创建新文件 以下是我目前使用的:Google apps script 我可以用应用程序脚本覆盖保存在Google Drive上的Excel文件(从Google工作表导出)的内容吗,google-apps-script,Google Apps Script,我使用Google Apps脚本将Google工作表作为Excel文件导出到Google Drive。然后,Excel文件通过Google Drive桌面应用程序与其他6名用户同步到其本地计算机。每次导出Google工作表时,它都会创建一个新文件,而不是替换旧文件(尽管文件名相同),并删除原始版本。这很好,只是当它与Google Drive Desktop同步时,Windows会删除原始版本并将其发送到回收站 该文件每15分钟创建一次,大小为3mb,因此几周后回收站就会充满千兆字节的数据 是否可
var blob = exportAsExcel(spreadsheetId)
var file = DriveApp.createFile(blob).setName(excelFileName);
fileId = file.getId();
file.makeCopy(destinationFolder);
Drive.Files.remove(fileId);
}
function exportAsExcel(spreadsheetId) {
var file = Drive.Files.get(spreadsheetId);
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
}
});
return response.getBlob();
}
您可以使用驱动器API覆盖文件。详细信息如下所示 我准备了一种将电子表格改写为现有excel文件的方法。此方法使用您的方法
exportAsExcel()
。首先,请确认是否在Google API控制台和高级Google服务上启用了驱动器API
src_sheetId
和dst_fileId
分别是要覆盖的电子表格ID和现有excel文件。通过运行overWrite()
,现有excel文件将被覆盖。因此excel文件的文件名和文件ID不变
脚本:
function overWrite(src_sheetId, dst_fileId) {
UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v3/files/" + dst_fileId + "?uploadType=multipart",
{
method: "PATCH",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
payload: exportAsExcel(src_sheetId).getBytes()
}
);
}
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var url = file.exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
顺便问一下,脚本的exportAsExcel()
工作正常吗?当我使用它时,会发生错误。我通过下面的修改删除了错误
发件人:
function overWrite(src_sheetId, dst_fileId) {
UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v3/files/" + dst_fileId + "?uploadType=multipart",
{
method: "PATCH",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
payload: exportAsExcel(src_sheetId).getBytes()
}
);
}
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var url = file.exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
至:
function overWrite(src_sheetId, dst_fileId) {
UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v3/files/" + dst_fileId + "?uploadType=multipart",
{
method: "PATCH",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
payload: exportAsExcel(src_sheetId).getBytes()
}
);
}
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var url = file.exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
如果我误解了您的问题,很抱歉。您可以使用驱动器API覆盖文件。详细信息如下所示 我准备了一种将电子表格改写为现有excel文件的方法。此方法使用您的方法
exportAsExcel()
。首先,请确认是否在Google API控制台和高级Google服务上启用了驱动器API
src_sheetId
和dst_fileId
分别是要覆盖的电子表格ID和现有excel文件。通过运行overWrite()
,现有excel文件将被覆盖。因此excel文件的文件名和文件ID不变
脚本:
function overWrite(src_sheetId, dst_fileId) {
UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v3/files/" + dst_fileId + "?uploadType=multipart",
{
method: "PATCH",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
payload: exportAsExcel(src_sheetId).getBytes()
}
);
}
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var url = file.exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
顺便问一下,脚本的exportAsExcel()
工作正常吗?当我使用它时,会发生错误。我通过下面的修改删除了错误
发件人:
function overWrite(src_sheetId, dst_fileId) {
UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v3/files/" + dst_fileId + "?uploadType=multipart",
{
method: "PATCH",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
payload: exportAsExcel(src_sheetId).getBytes()
}
);
}
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var url = file.exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
至:
function overWrite(src_sheetId, dst_fileId) {
UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v3/files/" + dst_fileId + "?uploadType=multipart",
{
method: "PATCH",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
payload: exportAsExcel(src_sheetId).getBytes()
}
);
}
var url = file.exportLinks['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'];
var url = file.exportLinks['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
如果我误解了你的问题,我很抱歉。作为@Tanaike回答的补充。您可以使用,这允许您在应用程序脚本中使用Google Drive web API。这也可以在相关的SO中找到。希望这对您有所帮助。@Mr.Rebot感谢您提供的更多信息。对延迟回复表示歉意我一直在工作。太棒了!!这是一种享受。我不知道http请求中的补丁方法。一直在学习!好东西,欢迎。也谢谢你。作为@Tanaike的补充回答。您可以使用,这允许您在应用程序脚本中使用Google Drive web API。这也可以在相关的SO中找到。希望这对您有所帮助。@Mr.Rebot感谢您提供的更多信息。对延迟回复表示歉意我一直在工作。太棒了!!这是一种享受。我不知道http请求中的补丁方法。一直在学习!好东西,欢迎。也谢谢你。