2010-07-29 11 views

Trả lời

9

Tôi đã viết Macro của riêng mình để giải quyết vấn đề này & đã chia sẻ nó. Bạn có thể chỉ định các trường trong mô hình của bạn, điền vào dữ liệu và YAML được tạo ra. Phần tốt nhất là nó hỗ trợ Nested liệu cũng (dựa trên NestedSet thuyết Behavior)

Bạn có thể tải các tập tin từ đây: http://www.prasadgupte.com/go/converting-csvexcel-data-to-doctrine-yaml-fixtures/

Hope this helps!

+0

UpVo tes không quan trọng cá nhân nhưng nó instils sự tự tin trong một câu trả lời cho người tìm kiếm. Hơn 300 lượt truy cập/lượt tải xuống và chỉ có 2 upvotes ở đây .. – Prasad

3

Một tìm kiếm google nhanh chóng đến với điều này: http://code.activestate.com/recipes/546518-simple-conversion-of-excel-files-into-csv-and-yaml/

Yêu cầu Python dù nhưng đó không phải là một vấn đề. Trông khá hứa hẹn và thực hiện chính xác những gì bạn cần (lưu ý rằng các tệp CSV có thể được mở bằng excel như một tệp excel gốc và được lưu dưới dạng .xls)

+0

nhờ DrColossos, tôi đã có bumped vào này ... nhưng chưa bao giờ làm việc với Python trước - thậm chí không biết những gì nó sẽ làm để chạy một kịch bản như thế này .. Tôi sẽ cố gắng để đạt được một cái gì đó đơn giản .. và gửi nó ở đây ... cảm ơn – Prasad

1

Nếu bạn đã sử dụng macro chuyển đổi, thì bạn có thể thêm hàm sẽ tạo tập lệnh PHP từ dữ liệu CSV. Vì vậy, một hàng dữ liệu cho các đối tượng "Pen" như: tên loại giá

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

sẽ đầu ra:

// 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); 

Dưới đây là chức năng vĩ mô:

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