2010-07-29 44 views
8

想知道是否可以輕鬆地將結構化文件轉換爲用於Doctrine/Symfony的YAML數據裝置。從數據庫/ Excel/CSV轉換到YAML數據裝置?

我沒有看到Doctrine接受CSV的任何實用工具。

我可能剛開始寫一些簡單的事來做到這一點。這是否值得?

回答

9

我寫了我自己的宏來解決這個問題&分享了它。您可以指定模型中的字段,填寫數據並生成YAML。 最好的部分是,它支持,以及(基於NestedSet主義行爲)嵌套數據

你可以從這裏下載文件: http://www.prasadgupte.com/go/converting-csvexcel-data-to-doctrine-yaml-fixtures/

希望這有助於!

+0

UpVo tes並不重要,但它爲尋求者的回答灌輸了信心。 300多次訪問/下載和2個upvotes在這裏.. – Prasad 2014-01-12 12:12:33

3

一個快速谷歌搜索想出了這個:http://code.activestate.com/recipes/546518-simple-conversion-of-excel-files-into-csv-and-yaml/

需要Python雖然但這不應該是一個問題。看起來很有前途,並且完全符合你的需求(請記住,CSV文件可以用本地excel文件打開並保存爲.xls)

+0

感謝DrColossos,我有碰到這個......但從來沒有用過Python的工作 - 甚至不知道將需要如何運行這樣的腳本..我會盡量實現一些簡單的..並將其張貼在這裏...謝謝 – Prasad 2010-07-30 08:06:52

1

如果您已經在使用轉換宏,那麼您可以添加一個函數,該函數將從CSV數據創建一個PHP腳本。因此,對於像對象 「筆」 數據行: 名稱型號價格

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