Thư viện QueriesChèn dữ liệu từ CSV vào bài viết Bricks
Chèn dữ liệu từ CSV vào bài viết Bricks
Query này phân tích dữ liệu CSV và chèn vào các phần tử text trong trang Bricks.
Query này yêu cầu tiện ích mở rộng Bricks phải được kích hoạt.
Query yêu cầu các biến sau:
$customPostId: ID của bài đăng tùy chỉnh Bricks cần cập nhật$csvFileURL: URL của tệp CSV cần phân tích
query InitializeGlobalVariables
@configureWarningsOnExportingDuplicateVariable(enabled: false)
{
emptyArray: _echo(value: [])
@export(as: "elementToUpdateIDs")
emptyNumber: _echo(value: 0)
@export(as: "numberCsvEntries")
}
query GetCSVData(
$url: URL!
$headingElementColumn: String! = "Title"
$textElementColumn: String! = "Description"
)
@depends(on: "InitializeGlobalVariables")
{
_sendHTTPRequest(input: {
url: $url,
method: GET
}) {
body
csv: _strParseCSV(
string: $__body
)
@underEachArrayItem(
passValueOnwardsAs: "csvPostEntry"
affectDirectivesUnderPos: [1, 3]
)
@underJSONObjectProperty(by: { key: $headingElementColumn })
@export(as: "csvHeadings")
@underJSONObjectProperty(by: { key: $textElementColumn })
@export(as: "csvTexts")
numberCsvEntries: _arrayLength(array: $__csv)
@export(as: "numberCsvEntries")
}
}
query ExportData($customPostId: ID!)
@depends(on: "GetCSVData")
{
customPost(by:{ id: $customPostId }, status: any) {
id
title
bricksDataTextElements: bricksData(filterBy: { include: ["text"] })
@arraySplice(offset: $numberCsvEntries)
@underEachArrayItem(
passIndexOnwardsAs: "index",
passValueOnwardsAs: "elementJSON"
affectDirectivesUnderPos: [1, 2, 3]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $elementJSON,
by: { key: "id" }
},
passOnwardsAs: "elementID"
)
@applyField(
name: "_arrayItem",
arguments: {
array: $csvTexts,
position: $index
},
passOnwardsAs: "csvText"
)
@applyField(
name: "_echo",
arguments: {
value: {
id: $elementID,
settings: {
text: $csvText
}
}
}
setResultInResponse: true
)
@export(as: "textMergeInputElements")
bricksDataHeadingElements: bricksData(filterBy: { include: ["heading"] })
@arraySplice(offset: $numberCsvEntries)
@underEachArrayItem(
passIndexOnwardsAs: "index",
passValueOnwardsAs: "elementJSON"
affectDirectivesUnderPos: [1, 2, 3]
)
@applyField(
name: "_objectProperty",
arguments: {
object: $elementJSON,
by: { key: "id" }
},
passOnwardsAs: "elementID"
)
@applyField(
name: "_arrayItem",
arguments: {
array: $csvHeadings,
position: $index
},
passOnwardsAs: "csvHeading"
)
@applyField(
name: "_echo",
arguments: {
value: {
id: $elementID,
settings: {
text: $csvHeading
}
}
}
setResultInResponse: true
)
@export(as: "headingMergeInputElements")
}
}
query AdaptData
@depends(on: "ExportData")
{
allMergeInputElements: _arrayMerge(
arrays: [$textMergeInputElements, $headingMergeInputElements]
)
@export(as: "allMergeInputElements")
}
mutation UpdateData($customPostId: ID!)
@depends(on: "AdaptData")
{
bricksMergeCustomPostElementDataItem(input: {
customPostID: $customPostId
elements: $allMergeInputElements
}) {
status
errors {
__typename
...on ErrorPayload {
message
@passOnwards(as: "message")
@fail(
message: $message
condition: ALWAYS
)
}
}
customPost {
__typename
...on CustomPost {
id
bricksData
}
}
}
}