-1

i'm trying to use class on Office Scripts (Excel Online). constructor() produces "Unexpected strict mode reserved word". could someone help me?

function main(workbook: ExcelScript.Workbook)
{
  class Foo{
    constructor(){
      console.log("Foo!");
    }
  }
  const foo = new Foo();
}

code editor screenshot "Unexpected strict mode reserved word"

  • 1
    Can you provide more details on how you are planning to use the class? Office Scripts has a few restrictions on top of TypeScript to ensure your script works consistently and as intended with your Excel workbook. If you can provide more details on your scenario, it will help in improving Office Scripts. Note that you can send feedback directly to Microsoft using the `Send feedback` option in the Script menu (opened by clicking `...`). Thanks – Jay Rathi - Microsoft Aug 05 '21 at 17:18
  • thanks jay. i couldn't figure out whether this is bug or restriction in the MS documentation [link](https://learn.microsoft.com/en-us/office/dev/scripts/develop/typescript-restrictions). it seems a sort of restriction. i would use handmade init() stuff instead of constructor() for the time being. – user16596625 Aug 07 '21 at 06:48

1 Answers1

0

It looks like that operation isn't allowed in the constructor. You can provide a string in the object's constructor, assign that to a private field in the class, and then print that string using a method. So something like:

  function main(workbook: ExcelScript.Workbook) {
class Foo {
  private foo : string
  constructor(foo) {
    this.foo = foo;
  }
  bar() {
    console.log(this.foo)
  }
}
const foo = new Foo("Foo!");
foo.bar() //Foo!

}

beyphy
  • 311
  • 2
  • 7