Ada*_*ace 19 class google-apps-script
我想在我的脚本中创建一个类.
Google Apps脚本语言基于javaScript,因此我从javaScript手册中获取了一个示例:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.我收到此错误消息:
Missing ; before statement. (line 1, file "Code")
Run Code Online (Sandbox Code Playgroud)
这是否意味着无法在谷歌脚本中创建新类?
或者我应该使用不同的语法?
Cam*_*rts 32
从历史上看,Javascript是一种"无类别"语言,类是一种尚未被广泛采用的新功能,而且Apps Script显然尚不支持.
以下是您可以在Apps脚本中模仿课堂行为的示例:
var Polygon = function(height, width){
this.height = height;
this.width = width;
this.logDimension = function(){
Logger.log(this.height);
Logger.log(this.width);
}
};
function testPoly(){
var poly1 = new Polygon(1,2);
var poly2 = new Polygon(3,4);
Logger.log(poly1);
Logger.log(poly2);
poly2.logDimension();
}
Run Code Online (Sandbox Code Playgroud)
TypeScript 现在可以与 GAS 一起使用。要使用它,请使用下载脚本文件,clasp然后通过将文件后缀更改为.ts. 更多信息可以在https://developers.google.com/apps-script/guides/typescript找到。完成后,class就可以使用了。
可以在https://github.com/november-yankee/science-test-grading找到用于实现 Google Sheets 自定义函数的 TypeScript 示例。