admin管理员组文章数量:1333706
I have about 15-20 Google Apps Script projects which all use the same list of global variables.
What I've done is defined all of the globals at the top of the first script file in the project, and then copied and pasted the block of code to the same spot in each project. So if I make a change in one, I copy and paste the entire thing from that one to the rest of them. It gets time-consuming.
Is there a better way to do this? Is it using Libraries? Does anyone use Libraries for defining globals across projects?
I have about 15-20 Google Apps Script projects which all use the same list of global variables.
What I've done is defined all of the globals at the top of the first script file in the project, and then copied and pasted the block of code to the same spot in each project. So if I make a change in one, I copy and paste the entire thing from that one to the rest of them. It gets time-consuming.
Is there a better way to do this? Is it using Libraries? Does anyone use Libraries for defining globals across projects?
Share Improve this question edited Sep 8, 2017 at 0:06 Employee asked Oct 8, 2014 at 23:18 EmployeeEmployee 2,4113 gold badges37 silver badges65 bronze badges1 Answer
Reset to default 8Using a library for shared constants is the most effective way to share constant objects between Google Apps Scripts. Some caveats:
- All scripts using the ConstLib will need to do so with "Development Mode" ON, otherwise you'll still need to update each of them manually. (Risk: save a buggy version of ConstLib and all your scripts will immediately break.)
The constants are attributes of the library, so will need to be referenced using the library name, e.g.
var log = SpreadsheetApp.openById( ConstLib.auditLogId );
In your existing scripts, you may find it convenient to change your block of existing constants into references to the ConstLib, so you won't need to touch the remaining code. e.g.
var auditLogId = ConstLib.auditLogId; . . . var log = SpreadsheetApp.openById( auditLogId );
Example
ConstLib
var roses = "Red", violets = "Blue";
Use Constlib
function myFunction() {
Logger.log(ConstLib.roses);
Logger.log(ConstLib.violets);
}
Logging Output
[14-10-09 14:51:47:258 EDT] Red
[14-10-09 14:51:47:259 EDT] Blue
版权声明:本文标题:javascript - Defining globals in Google Apps Script that can be used across multiple projects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742338963a2456206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论