0

我有一个类应该将 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 相关?

4

2 回答 2

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;
  }
}
于 2021-11-03T15:27:15.460 回答
1

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

解决此限制的一种方法是创建一个调用 Office 脚本 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;
  }
}

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

于 2021-11-04T21:37:51.820 回答