"Sample code: Return table data as JSON" does not work

芹沢 基樹 111 Reputation points
2022-05-26T08:38:58.703+00:00

Sample code: Return table data as JSON does not work, but it returns the following error when I try to run it.

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
object[objectKeys[j]] = values[i][j]

The sample office script can be found on
https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/get-table-data#sample-code-return-table-data-as-json

I think it was working before, any change on the spec?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,143 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yutao Huang - MSFT 701 Reputation points Microsoft Employee
    2022-05-26T20:14:11.867+00:00

    Hmm... it's indeed not working now. Before we can get an updated sample published, below is a temporary workaround. The only changes are in line 31 and 36.

    function main(workbook: ExcelScript.Workbook): TableData[] {
      // Get the first table in the "PlainTable" worksheet.
      // If you know the table name, use `workbook.getTable('TableName')` instead.
      const table = workbook.getWorksheet('PlainTable').getTables()[0];
    
      // Get all the values from the table as text.
      const texts = table.getRange().getTexts();
    
      // Create an array of JSON objects that match the row structure.
      let returnObjects: TableData[] = [];
      if (table.getRowCount() > 0) {
        returnObjects = returnObjectFromValues(texts);
      }
    
      // Log the information and return it for a Power Automate flow.
      console.log(JSON.stringify(returnObjects));
      return returnObjects
    }
    
    // This function converts a 2D array of values into a generic JSON object.
    // In this case, we have defined the TableData object, but any similar interface would work.
    function returnObjectFromValues(values: string[][]): TableData[] {
      let objectArray: TableData[] = [];
      let objectKeys: string[] = [];
      for (let i = 0; i < values.length; i++) {
        if (i === 0) {
          objectKeys = values[i]
          continue;
        }
    
        let object: {[key: string]: string} = {}
        for (let j = 0; j < values[i].length; j++) {
          object[objectKeys[j]] = values[i][j]
        }
    
        objectArray.push(object as unknown as TableData);
      }
    
      return objectArray;
    }
    
    interface TableData {
      "Event ID": string
      Date: string
      Location: string
      Capacity: string
      Speakers: string
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.