无法在Google Apps脚本表中使用类方法

时间:2019-07-16 13:51:24

标签: javascript typescript google-apps-script

我有两个用打字稿键入的文件,然后用.gs编译为Google应用脚本。当我调用此方法时,我有一类带有方法的辅助函数:flatten_我收到错误

TypeError: Cannot find function flatten_ in object function Utils() {...}.

我尝试过: 1.使flatten_为静态方法 2.制作flatten_静态属性 3.声明flatten_为公共函数 所有这些都没有解决。

backend.ts

import { Utils } from './utils/_';

function onOpen() {
    const ui = SpreadsheetApp.getUi();
    ui.createMenu('Tracker')
    .addItem('JUMP To Record', 'Jump')
    .addToUi();
}

function Jump() {
    const ui = SpreadsheetApp.getUi();
    const prompt = ui.prompt('Submit A Tracker ID');

    /** Once the user clicks ok */
    if(prompt.getSelectedButton() == ui.Button.OK) {
        /** If the tracker id length is less than 1 */
        if(prompt.getResponseText().length < 1) {
            Browser.msgBox('Submit A Valid Tracker Id');
        } else {
            /** Begin timer and find the tracker id record */
            console.time('Jump To Tracker Id Execution');
            const spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Client'); 
            const trackerRange = spreadsheet.getRange('1:1').getValues();
            const trackerColumn = trackerRange[0].indexOf('Tracker ID') + 1;
            const trackerIds = Utils.flatten_(spreadsheet.getRange(1, trackerColumn, spreadsheet.getLastRow(), 1).getValues());
            const recordIndex = trackerIds.indexOf(prompt.getResponseText());
            console.timeEnd('Submit A Valid Tracker Id');
            /** End timer and check record results */
            if(recordIndex == -1) {
                Browser.msgBox('Record ['+ prompt.getResponseText() + '] Could Not Be Found')
            } else {
                spreadsheet.setActiveRange(spreadsheet.getRange(recordIndex + 1, trackerColumn));
            }
        }
    }  
}

编译后的后端

// Compiled using ts2gas 3.4.4 (TypeScript 3.5.3)
var exports = exports || {};
var module = module || { exports: exports };
//import { Utils } from "./utils/_";

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Tracker')
        .addItem('JUMP To Record', 'Jump')
        .addToUi();
}
function Jump() {
    var ui = SpreadsheetApp.getUi();
    var prompt = ui.prompt('Submit A Tracker ID');
    /** Once the user clicks ok */
    if (prompt.getSelectedButton() == ui.Button.OK) {
        /** If the tracker id char length is less than 1 */
        if (prompt.getResponseText().length < 1) {
            Browser.msgBox('Submit A Valid Tracker Id');
        }
        else {
            /** Begin timer and find the tracker id record */
            console.time('Jump To Tracker Id Execution');
            var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Client');
            var trackerRange = spreadsheet.getRange('1:1').getValues();
            var trackerColumn = trackerRange[0].indexOf('Tracker ID') + 1;
            var trackerIds = Utils.flatten_(spreadsheet.getRange(1, trackerColumn, spreadsheet.getLastRow(), 1).getValues());
            var recordIndex = trackerIds.indexOf(prompt.getResponseText());
            console.timeEnd('Submit A Valid Tracker Id');
            /** End timer and check record results */
            if (recordIndex == -1) {
                Browser.msgBox('Record [' + prompt.getResponseText() + '] Could Not Be Found');
            }
            else {
                spreadsheet.setActiveRange(spreadsheet.getRange(recordIndex + 1, trackerColumn));
            }
        }
    }
}

_。ts

export class Utils {
    /**
     * flatten_ : Combines array of arrays of objects into a singular array of objects
     */
    flatten_(_:any): any {
        return [].concat.apply([], _);
    }

    guidlist_(_:any): any {
        let z_ = _.map(() => { return [this.guidcreate_()] });
        return z_;
    }

    guidcreate_(): any {
        let _ = () => {return (((1+Math.random())*0x10000)|0).toString(16).substring(1)};
        return (_()+_()+"-"+_()+"-"+_()+"-"+_()+"-"+_()+_()+_());
    }

    help():void {
        /** This function display a description of the class */
        Logger.log(`This file houses all the helper function. The functions here will be used more than once
        hence having a seperate file that contains them all. Reference the Docs for more info on an individual
         function. Updated: 7/16/2019.`)
    }
}

已编译_

// Compiled using ts2gas 3.4.4 (TypeScript 3.5.3)
var exports = exports || {};
var module = module || { exports: exports };

var Utils = /** @class */ (function () {
    function Utils() {
    }
    Utils.prototype.flatten_ = function (_) {
        /** Combines array of arrays of objects into a singular array of objects */
        return [].concat.apply([], _);
    };
    Utils.prototype.guidlist_ = function (_) {
        var _this = this;
        var z_ = _.map(function () { return [_this.guidcreate_()]; });
        return z_;
    };
    Utils.prototype.guidcreate_ = function () {
        var _ = function () { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); };
        return (_() + _() + "-" + _() + "-" + _() + "-" + _() + "-" + _() + _() + _());
    };
    return Utils;
}());
exports.Utils = Utils;

1 个答案:

答案 0 :(得分:1)

通过扩展我可以访问的类,我弄清楚了。

export class Utils {
    iArray: Array<any>
    constructor(_:Array<any>) {
        this.iArray = _;
    }
}

export class Flatten extends Utils {
    exe(): any {
        return [].concat.apply([], this.iArray);
    }
}
import { Flatten } from "./utils/_";

const trackerIds = new Flatten(spreadsheet.getRange(1, trackerColumn, spreadsheet.getLastRow(), 1).getValues()).exe();