我试图从Office获得一个特定的JSON输出,以便使用进行API调用。我从Power Automate收到的输出没有API文档所要求的格式(下面链接到API文档)。尝试修改脚本以获得所需的输出,但不幸的是,我只是从js开始,所以我无法确定我需要什么。
现在,输入必须来自Excel表。如果需要,我可以对excel表进行不同的格式化,但是输入必须来自Excel表。现在,Excel表如下所示:
这是我正在使用的Office脚本,来自以下博客文章:https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/get-table-data
function main(workbook: ExcelScript.Workbook): TableData[] {
// Get the first table in the "WithHyperLink" worksheet.
// If you know the table name, use `workbook.getTable('TableName')` instead.
const table = workbook.getWorksheet('WithHyperLink').getTables()[0];
// Get all the values from the table as text.
const range = table.getRange();
// Create an array of JSON objects that match the row structure.
let returnObjects: TableData[] = [];
if (table.getRowCount() > 0) {
returnObjects = returnObjectFromValues(range);
}
// Log the information and return it for a Power Automate flow.
console.log(JSON.stringify(returnObjects));
return returnObjects
}
function returnObjectFromValues(range: ExcelScript.Range): TableData[] {
let values = range.getTexts();
let objectArray : TableData[] = [];
let objectKeys: string[] = [];
for (let i = 0; i < values.length; i++) {
if (i === 0) {
objectKeys = values[i]
continue;
}
let object = {}
for (let j = 0; j < values[i].length; j++) {
// For the 4th column (0 index), extract the hyperlink and use that instead of text.
if (j === 4) {
object[objectKeys[j]] = range.getCell(i, j).getHyperlink().address;
} else {
object[objectKeys[j]] = values[i][j];
}
}
objectArray.push(object as TableData);
}
return objectArray;
}
interface TableData {
"Event ID": string
Date: string
Location: string
Capacity: string
"Search link": string
Speakers: string
}这是我运行Office脚本时在中获得的输出:
[
{
"Line": "",
"Id": "0",
"Description": "nov portion of rider insurance",
"Amount": "100",
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": "",
"PostingType": "Debit",
"AccountRef": "",
"value": "39",
"name": "Opening Bal Equity"
},
{
"Line": "",
"Id": "",
"Description": "nov portion of rider insurance",
"Amount": "100",
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": "",
"PostingType": "Credit",
"AccountRef": "",
"value": "44",
"name": "Notes Payable"
}
]但是,我需要的模式如下(它基于这个API文档https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/journalentry):
{
"Line": [
{
"Id": "0",
"Description": "nov portion of rider insurance",
"Amount": 100.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Debit",
"AccountRef": {
"value": "39",
"name": "Opening Bal Equity"
}
}
},
{
"Description": "nov portion of rider insurance",
"Amount": 100.0,
"DetailType": "JournalEntryLineDetail",
"JournalEntryLineDetail": {
"PostingType": "Credit",
"AccountRef": {
"value": "44",
"name": "Notes Payable"
}
}
}
]
}这里有很多不同之处,很明显,当我尝试进行API调用时,我会得到一个400‘坏请求’错误。有人知道我必须如何修改脚本或Excel表,或者在Power自动化中做一些不同的事情来获得我需要的特定模式吗?
任何帮助都将不胜感激。谢谢!!
发布于 2022-03-13 23:09:56
我认为您的脚本的核心是解析所有内容,以匹配您在教程中给出的线性"TableData“接口,然后将它发送给Stringify方法,但是您的数据与该接口不匹配,所以它会尽最大努力将每个单独的行输出到一个对象数组中。当Stringify被调用时,它会看到一个普通对象数组,因此它只是将所有内容转换为一个字符串。
我认为您希望这个结构更加结构化,这意味着您需要为每个行编写要传递的对象的代码。基本上,JSON模式告诉您的数据类型应该如下所示:
Interface AccountRefPart {
value: string
name: string
}
Interface JournalEntryLineDetailPart {
PostingType: string
AccountRef: AccountRefPart
}
Interface LinePart {
ID?: string
Description: string
Amount: number
DetailType: string
JournalEntryLineDetail: JournalEntryLineDetailPart
}
Interface TableData {
Line: LinePart[]
}如果您只想以JSON的形式传递单个Line元素(这是最外层的大括号所建议的),那么您将需要对TableData类型的单个对象进行字符串化,并且希望使用表行中的数据来构造这个对象。(我看不见你的桌子,但我相信它有你上面需要的信息。)
https://stackoverflow.com/questions/71456940
复制相似问题