[GastForen Programmierung/Entwicklung AppleScript excel2xml

  • Suche
  • Hilfe
  • Lesezeichen
  • Benutzerliste
Themen
Beiträge
Moderatoren
Letzter Beitrag

excel2xml

-hans-
Beiträge gesamt: 748

24. Apr 2010, 12:43
Beitrag # 1 von 2
Bewertung:
(2369 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo,

vielleicht nicht das schönste AS, aber es funktioniert ... ;-)

Das Script macht aus einem angegebenen Bereich in einem offenen Exceldokument eine xml-Datei. Der erste Eintrag einer Spalte dient als Tagg, Values somit ab Reihe 2. Jede Reihe wird als Gruppe zusammengefasst und durch Kommata getrennte Zelleneinträge werden separat getaggt ...

Vielleicht ist das Script ja noch für jemand anderen von Nutzen :)
Code
property NameCounter : 0 

--Bereich der Exceltabelle angeben der bearbeitet werden soll
set CountCols to display dialog "Anzahl Spalten angeben" default answer "10"
set MyCols to text returned of CountCols as number
set CountRows to display dialog "Anzahl Zeilen angeben" default answer "10"
set MyRows to text returned of CountRows as number

--Exceldaten in AS-Liste {{Reihe 1}, {Reihe 2}, usw.}
set {excel_data, dummy} to {{}, {}}
tell application "Microsoft Excel"
repeat with r_row from 1 to MyRows
repeat with c_col from 1 to MyCols
copy value of cell c_col of row r_row of active sheet to the end of dummy
end repeat
set end of excel_data to dummy
set dummy to {}
end repeat
end tell

--Excel2xml
set Counter to 1
set TaggedList to {}

--Datenbereich ab Reihe 2
--Der erste Wert einer Spalte wird als Tagg verwendet

repeat with i from 2 to count of excel_data
set Myrow to item i of excel_data

repeat with j from 1 to count of Myrow
set MyTagg to item j of item 1 of excel_data
set OpenTagg to "<" & MyTagg & ">"
set CloseTagg to "</" & MyTagg & ">"
set MyItem to item j of Myrow

(*
--Macht aus einem Wert 200.567 den string "200"
try
set MyItem to MyItem as integer
set MyItem to MyItem as text
set AppleScript's text item delimiters to ","
set MyItem to text item 1 of MyItem
end try
*)

--Durch Kommata getrennte Daten einer Zelle werden einzelnen getaggt, als Untergruppe
if MyItem contains ", " then
set AppleScript's text item delimiters to ", "
set end of TaggedList to OpenTagg
set MyNames to every text item of MyItem
set CountAgain to 1
repeat with OneName from 1 to count of MyNames
set NameCounter to (count of MyNames) + 1
set ThisName to item OneName of MyNames
set ThisName to "<name" & CountAgain & ">" & ThisName & "</name" & CountAgain & ">"
set end of TaggedList to ThisName
set CountAgain to CountAgain + 1
end repeat
set end of TaggedList to CloseTagg

else if MyItem does not contain ", " then
set MyItem to OpenTagg & MyItem & CloseTagg
set end of TaggedList to MyItem
if Counter is MyCols then
set Counter to 1
else if Counter is less than MyCols then
set Counter to Counter + 1
end if
end if
end repeat

end repeat

--Aus jeder Datenreihe wird eine Gruppe
set Counter to 1
set xmlData to {}
repeat with k from 1 to count of TaggedList
set MyContent to item k of TaggedList
if Counter is 1 then
set end of xmlData to "<Gruppe>"
set end of xmlData to MyContent
set Counter to Counter + 1
else if Counter is MyCols + NameCounter then
set end of xmlData to MyContent
set end of xmlData to "</Gruppe>"
set Counter to 1
else
set end of xmlData to MyContent
set Counter to Counter + 1
end if
end repeat

--Sart- und End-taggs
set AppleScript's text item delimiters to return
set xmlData to xmlData as text
set xmlData to "<?xml version=\"1.0\" encoding=\"utf-8\"?>" & return & "<Root>" & return & xmlData & return & "</Root>"

set AppleScript's text item delimiters to ""

--Die fertige xml-Datei wird geschrieben
set myFile to ((path to desktop) & "Mein.xml" as Unicode text)

my write_to_file(xmlData, myFile, false)
on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file as «class utf8» starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file

X

excel2xml

pjoern
Beiträge gesamt: 110

24. Apr 2010, 13:23
Beitrag # 2 von 2
Beitrag ID: #440096
Bewertung:
(2359 mal gelesen)
URL zum Beitrag
Beitrag als Lesezeichen
Hallo -hans-,
vielen Dank. :)


als Antwort auf: [#440094]