我正在从保存在SharePoint文档库中的excel文件中提取数据。我正在获取行,并且对数据结构感到困惑。我还不太熟悉typscript / javascript。
我要返回行:
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);
}
});
});
});
}
我得到的回报:
我尝试通过数组枚举:
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);
}
}
}
但是我不了解newValue
对象:
我对这种结构不了解什么?它看起来像3个嵌套数组。
答案 0 :(得分:3)
问题在于,当您到达这一点时:for (let value of row.values)
,value
实际上是一个数组。因此,无需执行value.values
。
此代码应该可以正常工作:
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);
}
}
}
希望有帮助!