从数据库/Excel/CSV 转换为 YAML 数据装置?

问题描述 投票:0回答:5

想知道是否有一个易于将结构化文件转换为 Doctrine / Symfony 的 YAML 数据装置。

我没有看到 Doctrine 有任何实用程序可以接受 CSV。

我可能会开始写一些简单的东西来做到这一点。值得吗?

excel csv symfony1 doctrine yaml
5个回答
10
投票

我编写了自己的宏来解决这个问题并分享它。您可以指定模型中的字段、填写数据并生成 YAML。 最好的部分是它也支持嵌套数据(基于 NestedSet Doctrine Behaviour)

您可以从这里下载文件: http://www.prasadgupte.com/go/converting-csvexcel-data-to-doctrine-yaml-fixtures/

希望这有帮助!


3
投票

快速谷歌搜索得出了这个:http://code.activestate.com/recipes/546518-simple-conversion-of-excel-files-into-csv-and-yaml/

虽然需要Python,但这应该不是问题。看起来很有前途,并且完全满足您的需要(请记住,CSV 文件可以像本机 Excel 文件一样使用 Excel 打开并另存为 .xls)


1
投票

如果您已经在使用转换宏,那么您可以添加一个函数,该函数将从 CSV 数据创建 PHP 脚本。因此对象“Pen”的数据行如下: 名称 类型 价格

Pen Name, Type, Price
"Reyballs Super Point 0.5", "Ball point", 10 
"Palkar Ink Pen", "Ink Pen", 25

将输出:

// Object: Pen
$pen1 = new Pen();
$pen1->name = "Reyballs Super Point 0.5";
$pen1->type = "Ball point";
$pen1->price = "10";
$pen1->save();
unset($pen1);
$pen2 = new Pen();
$pen2->name = "Palkar Ink Pen";
$pen2->type = "Ink Pen";
$pen2->price = "25";
$pen2->save();
unset($pen2);

这是宏函数:

Sub GeneratePHP()

targetSheetRow = 1
fieldNamesRow = 3
sourceSheetDataRow = fieldNamesRow + 1
earlyLoopEnd = False
counter = 0

' do not run without active sheet
If ActiveSheet.Name = "" Then
MsgBox "Please call the macro from a sheet"
End
End If

' identify sheets
Set source = ActiveSheet
' custom output sheet
targetSheetName = source.Cells(1, 12)

If targetSheetName = "" Or targetSheetName = "Output" Then
targetSheetName = "Output"
Else
On Error GoTo RTE
Set Target = Worksheets(targetSheetName)
GoTo RTS
RTE:
'MsgBox "PG" & Err.Description, Title:=Err.Source
targetSheetName = "Output"
End If

RTS:
' clear exsiting data in Target/Output sheet
Set Target = Worksheets(targetSheetName)
Target.Cells.Clear
Target.Cells.Font.Name = "Courier"
' Get no of fields in model (assume level & key always there)
noOfCols = 2
Do While source.Cells(fieldNamesRow, noOfCols + 1) <> "end"
noOfCols = noOfCols + 1
Loop
' If no field other than level & key, error
If noOfCols < 3 Then
MsgBox "No data for the records"
End
End If

' print Model name
Target.Cells(targetSheetRow, 1) = "// Object: " + source.Cells(1, 4)
targetSheetRow = targetSheetRow + 1
objClass = source.Cells(1, 4)

' Loop over data rows in source sheet
Do While source.Cells(sourceSheetDataRow, 1) <> "end"

If source.Cells(sourceSheetDataRow, 1) = "end-loop" Then
Target.Cells(targetSheetRow, 1) = "<?php endfor; ?>"
 targetSheetRow = targetSheetRow + 1
earlyLoopEnd = True
GoTo NextRow
End If

' rows to skip
If source.Cells(sourceSheetDataRow, 2) = "~!~" Or source.Cells(sourceSheetDataRow, 1) = "~!~" Then
GoTo NextRow
End If

' read level
blanks = source.Cells(sourceSheetDataRow, 1)

' print key
counter = counter + 1
varName = "$" + LCase(objClass) + CStr(counter)
varDec = varName + " = new " + objClass + "();"
Target.Cells(targetSheetRow, 1) = varDec
targetSheetRow = targetSheetRow + 1
spaces = spaces + "  "
spaces_count = spaces_count + 2

' print fields when value != ~!~
For clNumber = 3 To noOfCols
 If CStr(source.Cells(sourceSheetDataRow, clNumber)) <> "~!~" And CStr(source.Cells(fieldNamesRow, clNumber)) <> "~!~" Then
  Target.Cells(targetSheetRow, 1) = varName + "->" + source.Cells(fieldNamesRow, clNumber) + " = """ + CStr(source.Cells(sourceSheetDataRow, clNumber)) + """;"
  targetSheetRow = targetSheetRow + 1
 End If
Next clNumber

Target.Cells(targetSheetRow, 1) = varName + "->save();"
  targetSheetRow = targetSheetRow + 1
Target.Cells(targetSheetRow, 1) = "unset(" + varName + ");"
  targetSheetRow = targetSheetRow + 1

NextRow:
' go for next row in source sheet
sourceSheetDataRow = sourceSheetDataRow + 1

Loop

' Success
msg = "Data from sheet """ & source.Name & """ was converted to YAML on """ & targetSheetName & """ sheet" & vbCrLf & vbCrLf & "prasadgupte.com"
MsgBox msg
' Focus on output sheet
Sheets(targetSheetName).Select
Range("A1:A" & (targetSheetRow - 1)).Select
End Sub

1
投票

您可以尝试Data Transformer(免责声明 - 我是它的开发者)。它可以在本地进行 CSV、JSON、XML 和 YML 之间的转换。

它有许多转换设置(具有良好的默认值),因此您可以根据您的目的定制结果。

您可以从 Mac App StoreMicrosoft Store 获取。

没有试用版,但如果它不适合你,你可以要求苹果或微软退款!


0
投票

子ConvertTableToYAML() 昏暗的工作表 调暗最后一行只要 将 yamlString 调暗为字符串 将 serviceID 调暗为字符串 将电子邮件变暗为字符串 Dim emailArray() 作为字符串 暗淡 i 只要,j 只要

' Set the worksheet where your table is located (adjust as necessary)
Set ws = ThisWorkbook.Sheets("Sheet1")

' Get the last row in column A (assuming your service IDs are in column A and emails in column B)
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

' Initialize the YAML string
yamlString = "service_accounts:" & vbCrLf

' Loop through each row and build the YAML structure
For i = 2 To lastRow ' Start from row 2 to skip headers
    serviceID = ws.Cells(i, 1).Value
    emails = ws.Cells(i, 2).Value
    
    ' Split the comma-delimited email string into an array
    emailArray = Split(emails, ",")
    
    ' Append the service account and emails to the YAML string
    yamlString = yamlString & "  " & serviceID & ":" & vbCrLf
    yamlString = yamlString & "    emails:" & vbCrLf
    
    ' Loop through each email and add to YAML
    For j = LBound(emailArray) To UBound(emailArray)
        yamlString = yamlString & "    - " & Trim(emailArray(j)) & vbCrLf
    Next j
Next i

' Output the YAML string to a text file
Dim filePath As String
filePath = ThisWorkbook.Path & "\output.yaml"

Dim fileNumber As Integer
fileNumber = FreeFile
Open filePath For Output As fileNumber
Print #fileNumber, yamlString
Close fileNumber

' Notify the user
MsgBox "YAML file has been created at: " & filePath

结束子

© www.soinside.com 2019 - 2024. All rights reserved.