google-apps-script - 如何以编程方式将脚本文件从一个 google 工作表复制到另一个工作表?
问题描述
我为客户编写了一个应用程序,但大约有一百张需要完全相同的代码。手动将代码复制/粘贴到每个工作表的脚本编辑器中是否有捷径?
解决方案
这是一个示例:
我怀疑这是防弹的,所以它可能需要一些额外的工作,但它似乎对我有用。
它会启动一个对话框,允许您输入源和目标 ID 以及要复制的文件。请注意,目的地不能有同名的文件。
function appendSelectedFilesDialog() {
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('appendSelected1'), 'Append Selected Files');
}
function appendSelectedFilesIntoAnotherProject(obj) {
const srcProjectId = obj.srcProjectId;
const dstProjectId = obj.dstProjectId;
const srcFileNames = obj.srcFileNames;
var all = true;
var fA = [];
if (srcFileNames.length > 0) {
all = false;
fA = String(srcFileNames).split(',').map(n => n.trim());
}
if (srcProjectId && dstProjectId) {
const base1 = "https://script.googleapis.com/v1/projects/"
const url1 = base1 + srcProjectId + "/content";
const url2 = base1 + dstProjectId + "/content";
const options1 = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
const res1 = UrlFetchApp.fetch(url1, options1);
const data1 = JSON.parse(res1.getContentText());
const srcfiles = data1.files;
const options2 = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
const res2 = UrlFetchApp.fetch(url2, options2);
const data2 = JSON.parse(res2.getContentText());
const dstFiles = data2.files;
srcfiles.forEach(file => {
if (file.source && file.name) {
if (!all) {
if (fA.indexOf(file.name) > -1) {
dstFiles.push(file);
}
} else {
dstFiles.push(file);
}
}
});
const request = { "files": dstFiles };
const base = "https://script.googleapis.com/v1/projects/"
const url = base + dstProjectId + "/content"
const options = { "method": "put", "request": request, "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() }, "contentType": "application/json", "payload": JSON.stringify(request) };
const resp = UrlFetchApp.fetch(url, options);
console.log(resp);
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput('<textarea rols="12" cols="60">' + resp + '</textarea>'), "Update Script Project");
}
return { "message": "Process Complete" };//minimally to remove the dialog
}
html:
!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>input{margin:2px 5px 2px 0;}</style>
</head>
<body>
<form>
<input type="text" id="src" name="srcProjectId" size="60" placeholder="Enter Source Project Id" />
<br /><input type="text" id="dst" name="dstProjectId" size="60" placeholder="Enter Destination Project Id" />
<br /><input type="text" id="names" name="srcFileNames" size="60" placeholder ="Enter Desired Files separated by commas or nothing if you want all files." />
<br /><input type="button" value="Submit" onClick="process(this.parentNode);" />
</form>
<script>
function process(obj) {
google.script.run
.withSuccessHandler(function(obj){google.script.host.close();})
.appendSelectedFilesIntoAnotherProject(obj);
console.log('Form:',JSON.stringify(obj));
}
</script>
</body>
</html>
推荐阅读
- react-hooks - “错误:对象作为 React 子项无效”来自 useState
- node.js - 如何在 Heroku 上的 Node.js 应用程序上强制执行 SSL?
- rust - winit 事件循环在关机时抑制 Vulkan 验证输出
- email - Google Workspace 和 CPanel:发件人在向 CPanel 用户发送电子邮件时收到 550 错误
- reactjs - 如何使用刷新令牌配置 clientMaxAge/keepAlive?(下一个认证)
- google-sheets - 用 index() 填充电子表格列?
- formik - 是的:使用必须为“Y”的单个元素验证字符串数组
- javascript - 从内容页面访问母版页控件(html:输入)不起作用
- javascript - 什么是检查值是真值还是 0 的简洁方法?
- kubernetes - 部署模板中的 Kubernetes 探针