在转译的 Apps 脚本中导入 moment.js

Importing moment.js in transpiled Apps Script

我有必要在本地开发的 Apps Script 项目中使用 moment.js,该项目使用 google\clasp 从 TypeScript 转换。

我已经尝试像在.gs中正常编写时一样在项目中添加js库,但使用clasp似乎不允许我使用这种方式。

我已经尝试过像这样使用 eval 但没有成功:

eval(UrlFetchApp.fetch('library.min.js url').getContentText());

在所有情况下,在我推送到 Apps 脚本和 运行 Apps 脚本编辑器中的函数后,我收到了

ReferenceError: "moment" is not defined. (row 6, file "Test")

我的 TS 文件:

可以在 "standard" Apps 脚本项目中通过 evalUrlFetchApp 使用 Moment.js 等外部库,因为变量 exportsmodule 没有定义,所以库 installs itself into the global context

确实,我们可以通过在 Apps 脚本编辑器中检查 this 来验证结果:

Code.gs

var momentURL = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js";
function main() {
  eval(UrlFetchApp.fetch(momentURL).getContentText());
  Logger.log(this["moment"]);
}

执行main产生

function e() {
    return Qe.apply(null, arguments);
}

对于转译的 TypeScript,因为 exportsmodule 是全局定义的,eval'd 库的初始化假设它有一个更新的运行时/包管理系统由 Google Apps 脚本提供。

Code.ts

import * as moment from "moment";

const momentURL = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js";
function main() {
    eval(UrlFetchApp.fetch(momentURL).getContentText());
    Logger.log(`this['moment']: ${this["moment"]}`);
    Logger.log(`this.module: ${this.module}`);
    for (let key in this.module)
        Logger.log(`this.module[${key}]: ${this.module[key]}`);
}

$ clasp push -->

Code.gs

// Compiled using ts2gas 1.6.0 (TypeScript 3.2.2)
var exports = exports || {};
var module = module || { exports: exports };
//import * as moment from "moment";
var momentURL = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.4/moment.min.js";
function main() {
    eval(UrlFetchApp.fetch(momentURL).getContentText());
    Logger.log("this['moment']: " + this["moment"]);
    Logger.log("this.module: " + this.module);
    for (var key in this.module)
        Logger.log("this.module[" + key + "]: " + this.module[key]);
}

这会产生

的对数
this['moment']: undefined
this.module: [object Object]
this.module[exports]: 
function e() {
    return Qe.apply(null, arguments);
}

解决方案

因此 eval 成功,但绑定到 module.exports 而不是 moment。您可以(在转译的 Apps 脚本中)引用 module.exports 而不是 moment:

Logger.log(module.exports().format("YYYY")); // 2019

您可能需要使用与 clasp 不同的方法,因为 ts2gas(自 v1.6.0 起)似乎不支持 import / export transpiling。这种观察到的行为,其中转译的 TS 中的 module.exports 是 eval 的导入,这似乎非常脆弱,并且在编写实际的 TypeScript 时肯定不容易解决。

相关: