使用 Apps 脚本以 Excel 格式将 Google Sheet 导出到 Google 驱动器
Export a Google Sheet to Google Drive in Excel format with Apps Script
我想将我的 Google 电子表格备份到我的 Google Drive 文件夹中,但作为 Excel 文件。
我设法创建了一个代码来创建 gsheet 的副本并将其保存到文件夹中,但我无法更改代码以将其保存为 Excel 文件。
你能帮我解决一下吗?
function makeCopy() {
var formattedDate = Utilities.formatDate(new Date(), "CET", "yyyy-MM-dd' 'HH:mm");
var name = "Backup Copy " + formattedDate;
var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
var file = DriveApp.getFileById("2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c")
file.makeCopy(name, destination);
}
这个答案怎么样?我认为您的情况有几个答案。所以请将此视为其中之一。
为了将电子表格转换为 excel,可以使用 https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx
的端点。在这个答案中,使用了这个。
修改脚本:
function makeCopy() {
var formattedDate = Utilities.formatDate(new Date(), "CET", "yyyy-MM-dd' 'HH:mm");
var name = "Backup Copy " + formattedDate;
var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
// Added
var sheetId = "2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c";
var url = "https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx&access_token=" + ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url).getBlob().setName(name + ".xlsx"); // Modified
destination.createFile(blob);
}
如果我误解了你的问题,我很抱歉。
更新时间:2020 年 2 月 7 日
从 2020 年 1 月起,访问令牌不能与 access_token=###
等查询参数一起使用。 Ref 所以请使用请求的访问令牌 header 而不是查询参数。如下
var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
只是想将 Tanaike 的回答标记为团队并展示如何在通话期间将身份验证添加到 header。
function convertSheetToXLSX() {
var sheetId = "2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c";
var spreadsheetName = "My Spreadsheet";
var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + sheetId + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(spreadsheetName + ".xlsx");
destination.createFile(blob);
}
我刚刚添加了一个简单的 UI 以使其作为日常工具更有用。顶部脚本几乎是 MattMcCode 代码的直接副本。所以给他归属地。
function exportSpreadsheetToXLSX(obj) {
if (obj) {
console.log(JSON.stringify(obj));
var sheetId = obj.ssid;
var spreadsheetName = obj.filename;
var destination = DriveApp.getFolderById(obj.desid);
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + sheetId + "&exportFormat=xlsx";
var params = {
method: "get",
headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() },
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(spreadsheetName + ".xlsx");
destination.createFile(blob);
}
}
只需执行以下对话框,输入您的 ssid、folderid 和文件名
function openSpreadsheetExportToXLSXDialog() {
let html = '';
html += '<html><head><style> input{margin: 2px 5px 2px 0;}</style></head><body>';
html += '<form>';
html += '<input type="text" name="ssid" size="50" placeholder="Source Spreadsheet Id" /><br />';
html += '<input type="text" name="desid" size="50" placeholder="Destination Folder Id" /><br />';
html += '<input type="text" name="filename" size="50" placeholder="Spreadsheet ID" /><br />';
html += '<input type="button" value="Submit" onClick="processForm(this.parentNode)" />';
html += '<input type="button" value="Exit" onClick="google.script.host.close();" />';
html += '</form>';
html += '<script>';
html += 'function processForm(obj){console.log(obj);google.script.run.withSuccessHandler((obj)=>{google.script.host.close();}).exportSpreadsheetToXLSX(obj);}';
html += '</script></body></html>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'Spreadsheet Export to XLSX Dialog');
}
我想将我的 Google 电子表格备份到我的 Google Drive 文件夹中,但作为 Excel 文件。 我设法创建了一个代码来创建 gsheet 的副本并将其保存到文件夹中,但我无法更改代码以将其保存为 Excel 文件。
你能帮我解决一下吗?
function makeCopy() {
var formattedDate = Utilities.formatDate(new Date(), "CET", "yyyy-MM-dd' 'HH:mm");
var name = "Backup Copy " + formattedDate;
var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
var file = DriveApp.getFileById("2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c")
file.makeCopy(name, destination);
}
这个答案怎么样?我认为您的情况有几个答案。所以请将此视为其中之一。
为了将电子表格转换为 excel,可以使用 https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx
的端点。在这个答案中,使用了这个。
修改脚本:
function makeCopy() {
var formattedDate = Utilities.formatDate(new Date(), "CET", "yyyy-MM-dd' 'HH:mm");
var name = "Backup Copy " + formattedDate;
var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
// Added
var sheetId = "2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c";
var url = "https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx&access_token=" + ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url).getBlob().setName(name + ".xlsx"); // Modified
destination.createFile(blob);
}
如果我误解了你的问题,我很抱歉。
更新时间:2020 年 2 月 7 日
从 2020 年 1 月起,访问令牌不能与 access_token=###
等查询参数一起使用。 Ref 所以请使用请求的访问令牌 header 而不是查询参数。如下
var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
只是想将 Tanaike 的回答标记为团队并展示如何在通话期间将身份验证添加到 header。
function convertSheetToXLSX() {
var sheetId = "2SqIXLiic6-gjI2KwQ6OIgb-erbl3xqzohRgE06bfj2c";
var spreadsheetName = "My Spreadsheet";
var destination = DriveApp.getFolderById("1vFL98cgKdMHLNLSc542pUt4FMRTthUvL");
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + sheetId + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(spreadsheetName + ".xlsx");
destination.createFile(blob);
}
我刚刚添加了一个简单的 UI 以使其作为日常工具更有用。顶部脚本几乎是 MattMcCode 代码的直接副本。所以给他归属地。
function exportSpreadsheetToXLSX(obj) {
if (obj) {
console.log(JSON.stringify(obj));
var sheetId = obj.ssid;
var spreadsheetName = obj.filename;
var destination = DriveApp.getFolderById(obj.desid);
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + sheetId + "&exportFormat=xlsx";
var params = {
method: "get",
headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() },
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(spreadsheetName + ".xlsx");
destination.createFile(blob);
}
}
只需执行以下对话框,输入您的 ssid、folderid 和文件名
function openSpreadsheetExportToXLSXDialog() {
let html = '';
html += '<html><head><style> input{margin: 2px 5px 2px 0;}</style></head><body>';
html += '<form>';
html += '<input type="text" name="ssid" size="50" placeholder="Source Spreadsheet Id" /><br />';
html += '<input type="text" name="desid" size="50" placeholder="Destination Folder Id" /><br />';
html += '<input type="text" name="filename" size="50" placeholder="Spreadsheet ID" /><br />';
html += '<input type="button" value="Submit" onClick="processForm(this.parentNode)" />';
html += '<input type="button" value="Exit" onClick="google.script.host.close();" />';
html += '</form>';
html += '<script>';
html += 'function processForm(obj){console.log(obj);google.script.run.withSuccessHandler((obj)=>{google.script.host.close();}).exportSpreadsheetToXLSX(obj);}';
html += '</script></body></html>';
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),'Spreadsheet Export to XLSX Dialog');
}