我有一個從谷歌 Appsheet 更新的谷歌表格 - 完美運行。是否有一種方法/腳本可以添加到谷歌作業表中,它會自動更新谷歌驅動器或本地硬碟上的 Excel 作業表。(只有一個檔案/覆寫)
感謝您的任何見解謝謝
uj5u.com熱心網友回復:
關于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 Apps Script,還沒有直接實作覆寫本地PC檔案的方法。但是,對于 Google Drive 上的檔案,您可以覆寫該檔案。
在這個答案中,我想建議使用 Google Apps 腳本覆寫 Google Drive 上的檔案。
根據您的問題,我相信您的目標如下。
- 您想通過使用 Google Apps 腳本將電子表格轉換為 XLSX 資料來覆寫 Google Drive 上的現有 XLSX 檔案。
在這種情況下,下面的示例腳本怎么樣?
示例腳本:
此示例腳本使用 Drive API。因此,請在 Advanced Google services 中啟用 Drive API。并且,請設定Google Drive的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.
}
}
- 運行此腳本時,當 XLSX 檔案
xlsxFileId存在時,XLSX 檔案將被srcSpreadsheetId. - 運行此腳本時,如果 XLSX 檔案
xlsxFileId不存在,則會創建一個新的 XLSX 檔案。- 在這種情況下,作為示例,將在 Google Drive 的根檔案夾中創建一個新的 XLSX 檔案。
- 創建新的 XLSX 檔案后,您可以在日志中看到檔案 ID。請將此 ID 設定為 的變數
xlsxFileId。這樣,從下一次運行開始,輸入 ID 的 XLSX 檔案將被覆寫。
參考:
- 檔案:更新
uj5u.com熱心網友回復:
嘗試
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://www.uj5u.com/qiye/455071.html
上一篇:如何從SUMIFS中排除多個字串
下一篇:一次將回圈范圍限制為100行
