将数据表转换为Google Sheets API Json格式

8ehkhllq  于 2022-12-30  发布在  Go
关注(0)|答案(1)|浏览(131)

我正在尝试将Powerautomate中的DataTable转换为Json格式。
运行后,电源自动化中的我的数据表将如下所示:

我在Power Automation中运行这段代码

SET NewVar TO {{ }}
SET NewVar['CSVTable'] TO CSVTable
Variables.ConvertCustomObjectToJson CustomObject: NewVar Json=> SpreadsheetData

转换为此格式的Json的DataTable。

{"CSVTable":[
{
"Column#1":"Account Number:","Column#2":"???","Column#3":null,"Column#4":null,"Column#5":null,"Column#6":null,"Column#7":null,"Column#8":null,"Column#9":null,"Column#10":null,"Column#11":null,"Column#12":null
},
{...}
]

我试图转换为谷歌表API JSON格式像这样.

{
"range":"Sheet1A1:D5",
"majorDimension":"ROWS".
"values":[
["Account Number","???",null,null,...],
["Account Name","???",null,null,...]
],
}

此代码来自Google Sheets API
我的代码是错误的,如何修改它?

SET NewVar TO {{ }}
SET NewVar['CSVTable'] TO CSVTable
Variables.ConvertCustomObjectToJson CustomObject: NewVar Json=> SpreadsheetData
tzdcorbm

tzdcorbm1#

我自己找到了答案,虽然不是直接的解决方法,我用了excelVBA来帮助我,主要是启动ExcelMacro函数,把数据组织成我想要的方式
以下是PAD代码的一部分:

Excel.LaunchExcel.LaunchAndOpen Path: CurrentItem Visible: True ReadOnly: False LoadAddInsAndMacros: True Instance=> ExcelInstance2
Excel.RunMacro Instance: ExcelInstance2 Macro: $'''vba_merge'''
Excel.CloseExcel.CloseAndSave Instance: ExcelInstance2
File.ReadFromCSVFile.ReadCSV CSVFile: CurrentItem Encoding: File.CSVEncoding.UTF8 TrimFields: True FirstLineContainsColumnNames: False ColumnsSeparator: File.CSVColumnsSeparator.SystemDefault CSVTable=> CSVTable2
SET SpreadsheetData TO $'''{
  \"range\": \"%CurrentItem2[2]%!A1:K999\",
  \"majorDimension\": \"COLUMNS\",
  \"values\": [
    [%CSVTable2[0]['Column#1']%],
    [%CSVTable2[0]['Column#2']%],
    [%CSVTable2[0]['Column#3']%],
    [%CSVTable2[0]['Column#4']%],
    [%CSVTable2[0]['Column#5']%],
    [%CSVTable2[0]['Column#6']%],
    [%CSVTable2[0]['Column#7']%],
    [%CSVTable2[0]['Column#8']%],
    [%CSVTable2[0]['Column#9']%],
    [%CSVTable2[0]['Column#10']%],
    [%CSVTable2[0]['Column#11']%]
  ]
}'''

下面是Excel VBA的代码:这个的主要功能是把所有的行合并成列,并以Json格式排列,当然这个ExcelVba太简单粗暴了,可以继续优化,减少代码。

Sub vba_merge()

Application.DisplayAlerts = False

Dim val As String
Dim rng As Range
Dim Cell As Range
Set rng = Range("A1:A999")
For Each Cell In rng
    val = val & Chr(34) & Cell.Value & Chr(34) & ","
Next Cell
With rng
    .merge
    .Value = Trim(val)
End With

Dim val2 As String
Dim rng2 As Range
Dim Cell2 As Range
Set rng2 = Range("B1:B999")
For Each Cell2 In rng2
    val2 = val2 & Chr(34) & Cell2.Value & Chr(34) & ","
Next Cell2
With rng2
    .merge
    .Value = Trim(val2)
End With

Dim val3 As String
Dim rng3 As Range
Dim Cell3 As Range
Set rng3 = Range("C1:C999")
For Each Cell3 In rng3
    val3 = val3 & Chr(34) & Cell3.Value & Chr(34) & ","
Next Cell3
With rng3
    .merge
    .Value = Trim(val3)
End With

Dim val4 As String
Dim rng4 As Range
Dim Cell4 As Range
Set rng4 = Range("D1:D999")
For Each Cell4 In rng4
    val4 = val4 & Chr(34) & Cell4.Value & Chr(34) & ","
Next Cell4
With rng4
    .merge
    .Value = Trim(val4)
End With

Dim val5 As String
Dim rng5 As Range
Dim Cell5 As Range
Set rng5 = Range("E1:E999")
For Each Cell5 In rng5
    val5 = val5 & Chr(34) & Cell5.Value & Chr(34) & ","
Next Cell5
With rng5
    .merge
    .Value = Trim(val5)
End With

Dim val6 As String
Dim rng6 As Range
Dim Cell6 As Range
Set rng6 = Range("F1:F999")
For Each Cell6 In rng6
    val6 = val6 & Chr(34) & Cell6.Value & Chr(34) & ","
Next Cell6
With rng6
    .merge
    .Value = Trim(val6)
End With

Dim val7 As String
Dim rng7 As Range
Dim Cell7 As Range
Set rng7 = Range("G1:G999")
For Each Cell7 In rng7
    val7 = val7 & Chr(34) & Cell7.Value & Chr(34) & ","
Next Cell7
With rng7
    .merge
    .Value = Trim(val7)
End With

Dim val8 As String
Dim rng8 As Range
Dim Cell8 As Range
Set rng8 = Range("H1:H999")
For Each Cell8 In rng8
    val8 = val8 & Chr(34) & Cell8.Value & Chr(34) & ","
Next Cell8
With rng8
    .merge
    .Value = Trim(val8)
End With

Dim val9 As String
Dim rng9 As Range
Dim Cell9 As Range
Set rng9 = Range("I1:I999")
For Each Cell9 In rng9
    val9 = val9 & Chr(34) & Cell9.Value & Chr(34) & ","
Next Cell9
With rng9
    .merge
    .Value = Trim(val9)
End With

Dim val10 As String
Dim rng10 As Range
Dim Cell10 As Range
Set rng10 = Range("J1:J999")
For Each Cell10 In rng10
    val10 = val10 & Chr(34) & Cell10.Value & Chr(34) & ","
Next Cell10
With rng10
    .merge
    .Value = Trim(val10)
End With

Dim val11 As String
Dim rng11 As Range
Dim Cell11 As Range
Set rng11 = Range("K1:K999")
For Each Cell11 In rng11
    val11 = val11 & Chr(34) & Cell11.Value & Chr(34) & ","
Next Cell11
With rng11
    .merge
    .Value = Trim(val11)
End With

Application.DisplayAlerts = True
End Sub

这里提供了这个想法,如果有更简单的方法,欢迎留言

相关问题