【问题标题】:Office script "failed to load [code] [library]" errorOffice 脚本“无法加载 [代码] [库]”错误
【发布时间】:2021-11-03 14:31:46
【问题描述】:

我有一个类应该将 excel 表的值保留为属性。如果我将getValues() 放入类构造函数中,则脚本将失败并出现 failed to load [code] [library] 错误(来自编辑器控制台输出的完整消息)。

function main(wkb: ExcelScript.Workbook){
  const tbl = wkb.getTable('test')
  const info = new Info(tbl)
}

class Info {
  table: ExcelScript.Table
  vals: string[][]

  constructor(table: ExcelScript.Table){
    this.table = table
    // failing here, line commented for workaround
    this.vals = table.getValues() as unknown[][] as string[][]
  }

  getVals(){
    // this will work (if not called from the constructor)
    this.vals = this.table.getRange().getValues() as unknown[][] as string[][]
  }
}

作为一种解决方法,我在类实例初始化后调用getVals() 方法,但我想避免这种额外的调用。

我在这里做错了什么?是与 TypeScript 的工作原理(刚开始学习)有关还是与 Office Script API 相关?

【问题讨论】:

标签: typescript office-scripts excel-online


【解决方案1】:

您遇到的错误可能是办公脚本错误。

除了上面列出的选项之外,您还可以在创建对象时修改构造函数并提供表值。这将允许您在构造函数中分配表值:

function main(wkb: ExcelScript.Workbook) {
  const tbl : ExcelScript.Table = wkb.getTable('test');
  const tblValues: (string | number | boolean)[][] = tbl.getRangeBetweenHeaderAndTotal().getValues();
  const info = new Info(tbl,tblValues);
  console.log(info.getVals());
}

class Info {
  private table: ExcelScript.Table;
  private vals: string[][];

  constructor(table: ExcelScript.Table, vals: (string|number|boolean)[][]) {
    this.table = table;
    this.vals = vals as string[][];
  }

  getVals() {
    return this.vals;
  }
}

【讨论】:

    【解决方案2】:

    这是 Office 脚本 API 的当前限制。目前,请避免在类构造函数中调用 Office Script API。

    解决此限制的一种方法是创建一个调用 Office Script API 的方法:

    function main(workbook: ExcelScript.Workbook)
    {
      const info = getInfo(workbook);
      console.log(JSON.stringify(info.getVals()));
    }
    
    function getInfo(workbook: ExcelScript.Workbook) {
      const tbl: ExcelScript.Table = workbook.getTable('test');
      const tblValues: (string | number | boolean)[][] = tbl.getRangeBetweenHeaderAndTotal().getValues();
      const info = new Info(tbl, tblValues);
      return info;
    }
    
    class Info {
      private table: ExcelScript.Table;
      private vals: string[][];
    
      constructor(table: ExcelScript.Table, vals: (string | number | boolean)[][]) {
        // avoid calling Office Script APIs inside of class constructors.
        this.table = table;
        this.vals = vals as string[][];
      }
    
      getVals() {
        return this.vals;
      }
    }
    
    

    这个方法可以作为类的静态方法添加:

    function main(workbook: ExcelScript.Workbook)
    {
      const info = Info.create(workbook);
      console.log(JSON.stringify(info.getVals()));
    }
    
    class Info {
      private table: ExcelScript.Table;
      private vals: string[][];
    
      constructor(table: ExcelScript.Table, vals: (string | number | boolean)[][]) {
        // avoid calling Office Script APIs inside of class constructors.
        this.table = table;
        this.vals = vals as string[][];
      }
    
      static create(workbook: ExcelScript.Workbook) {
        const tbl: ExcelScript.Table = workbook.getTable('test');
        const tblValues: (string | number | boolean)[][] = tbl.getRangeBetweenHeaderAndTotal().getValues();
        const info = new Info(tbl, tblValues);
        return info;
      }
    
      getVals() {
        return this.vals;
      }
    }
    

    一般而言,就编程最佳实践而言,构造函数避免包含复杂逻辑可能会很方便。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多