最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Defining globals in Google Apps Script that can be used across multiple projects - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

Using 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
发布评论

评论列表(0)

  1. 暂无评论