2

I'm working on pulling data from an excel file saved on SharePoint Document Library. I'm getting the rows and I'm confused at the data structure. I'm still fairly new to typscript/javascript.

I'm getting the rows back:

private _getRows(documentLibraryID: string, documentID: string, worksheet: string, table: string): Promise<[MicrosoftGraph.WorkbookTableRow]> {
  return new Promise<any>((resolve, reject) => {
    this.props.context.msGraphClientFactory
    .getClient()
      .then((client: MSGraphClient): void => {
        client
          .api(`/sites/${URL}/drives/${documentLibraryID}/items/${documentID}/workbook/worksheets('${worksheet}')/Tables('${table}')/rows`)
          .get((error, response: any, rawResponse?: any) => {
            if (response) {
              let rows:[MicrosoftGraph.WorkbookTableRow] = response.value;

              resolve(rows);
            }
            else {
              reject(error);
            }
          });
        });
  });
}

What I get back:

enter image description here

I try to enumerate through the arrays:

  for (let row of rows) {
    console.log("row: ", row);
    for (let value of row.values) {
      console.log("value: ", value);
      for (let newValue of value.values) {
        console.log("newValue: ", newValue);

      }
    }
  }

But I don't get to the newValue object:

enter image description here

What don't I understand about this structure? It looks like 3 nested arrays.

Holden1515
  • 659
  • 2
  • 7
  • 20
  • https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it – Jonas Wilms Oct 15 '18 at 20:05
  • @JonasWilms I'm sorry, what are you trying to say? The object I'm trying to use is returned from Microsoft's Graph API, so I have no control over that. Are you saying something about my `_getRows` method and how I am handling the promise? I'm happy to receive any feedback on my code structuring, i'm pretty new to Typescript/Javascript. – Holden1515 Oct 15 '18 at 20:08
  • I guess 3 mins weren't enough to read the answers ... – Jonas Wilms Oct 15 '18 at 20:09
  • @JonasWilms You're right. I re-read it and I'm still not sure how to improve my promise chaining. – Holden1515 Oct 15 '18 at 20:19
  • You should return the `new Promise` from inside the `then`, that way if `.getClient()` rejects it can be handled too – Jonas Wilms Oct 15 '18 at 20:23

1 Answers1

3

The problem is that when you're reaching to this point: for (let value of row.values), value is indeed an array. So there's no need of doing value.values.

This code should work fine:

for (let row of rows) {
    console.log("row: ", row);
    for (let valueArr of row.values) {
      console.log("value: ", valueArr);
      for (let newValue of valueArr) {
        console.log("newValue: ", newValue);

      }
    }
  }

Hope it helps!

Rodrigo Ferreira
  • 1,091
  • 8
  • 11