MacUser.de Community!  

  weitere Suchoptionen
Zurück   MacUser.de Community! > Mac OS > Applescript und Automator

CSV Dateien per Applescript in Excel/Numbers importieren

Antwort
 
Themen-Optionen
Alt 08-02-2010, 13:46   #1 (permalink)
MU Mitglied
 
Benutzerbild von Babaganoush
 
Registriert seit: 06.2007
Beiträge: 162
CSV Dateien per Applescript in Excel/Numbers importieren

Hallo,

ich habe ein Riesenproblem:

Ich habe unzählige Ordner in denen jeweils knapp 70 *.csv Dateien liegen.
Diese 70 sollen in einer Excel/Numbers Tabelle Zusammengefasst werden. Die CSV Dateien sind dabei stets gleich Aufgebaut.

Ich habe bereits ein "halbes script" geschrieben bzw. mir zusammengebastelt.

Dass Problem ist allerdings, dass ich z.Z. noch das Skript für jede Datei neu anwerfen muss, es fehlt also eine Schleife. Kann mir da jemand ein bisschen helfen?


Die Dateien liegen immer im gleichen Ordner und sind folgendermaßen bezeichnet:

report.csv
report-1.csv
report-2.csv
...


Zweites Problem:

Das Skript bricht den Vorgang ab und schreibt nur bis zu einer bestimmten Stelle die Sachen in die Tabelle, wieso weiß ich leider nicht. Deswegen habe ich als Notbehelf die Zeichenanzahl begrenzt, dadurch bricht das Skript nur noch in der Hälfte der Fälle ab.

Drittes Problem :

Aktuell wird für jede Datei eine neue Tabelle in der Aktuellen Arbeitsmappe angelegt, ist es auch möglich alles in der gleichen Tabelle fortlaufend untereinander zu schreiben?

Hier mal mein bisheriger Code:
Code:
display dialog "Datei angeben:" default answer "Macintosh HD:Users:Ich:Downloads:Temp:report-1.csv"
set Datei to text returned of result

tell application "Numbers"
	
	activate
	
	tell document (count of documents)
		
		tell sheet 1
			
			tell application "Finder"
				set quoteLines to paragraphs of (read file Datei from 1 to 530)
			end tell
			
			set quoteValues to {}
			
			set nRows to length of quoteLines
			
			set tempLine to item 1 of quoteLines
			
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to ","
			
			set headers to every text item of tempLine
			set nCols to length of headers
			
			make new table with properties {name:"Daten", row count:36, column count:2}
			
			tell table (count of tables)
				
				set nRow to 1
				repeat with aLine in quoteLines
					
					set cellValues to every text item of aLine
					
					set nCol to 1
					
					repeat with aCell in cellValues
						
						set value of cell nCol of row nRow to aCell
						set nCol to nCol + 1
						
					end repeat
					
					set nRow to nRow + 1
					
				end repeat
				
			end tell
			
			set AppleScript's text item delimiters to oldDelims
			
		end tell
		
	end tell
	
end tell
Anbei auch mal zwei Beispieldateien. Bei der ersten bricht das Script stets ab, wobei es mir nur auf die Tagesdaten ankommt, alles davor und danach soll eh wegbleiben. Die zweite Datei funktioniert ohne Fehler.


Ich wäre euch wirklich suuuper dankbar wenn ihr mir helfen könntet
Angehängte Dateien
Dateityp: zip report-1.csv.zip (901 Bytes, 13x aufgerufen)
Dateityp: zip report-2.csv.zip (908 Bytes, 2x aufgerufen)
Babaganoush ist offline   Mit Zitat antworten
Alt 10-02-2010, 23:54   #2 (permalink)
MU Mitglied
 
Benutzerbild von iScript
 
Registriert seit: 02.2007
Beiträge: 127
ich hab mir dafür einen handler dafür gebastelt, den ich aber hier auf dem rechner nicht zur verfügung habe. ich grab ihn mal aus und poste ihn dann.

zu deinem script. hier mal etwas überarbeitet:

set Datei to (choose file)
set quoteLines to paragraphs of (read Datei from 1 to 530)

tell application "Numbers"
   activate
   tell document (count of documents)
      tell sheet 1
         set AppleScript's text item delimiters to ","
         make new table with properties {name:"Daten", row count:count quoteLines, column count:count text items of item 1 of quoteLines}
         tell table (count of tables)
            set nRow to 1
            repeat with aLine in quoteLines
               set cellValues to every text item of aLine
               set nCol to 1
               repeat with aCell in cellValues
                  set value of cell nCol of row nRow to aCell
                  set nCol to nCol + 1
               end repeat
               set nRow to nRow + 1
            end repeat
         end tell
         set AppleScript's text item delimiters to ""
      end tell
   end tell
end tell

Geändert von iScript (11-02-2010 um 00:09 Uhr).
iScript ist offline   Mit Zitat antworten
Alt 11-02-2010, 21:40   #3 (permalink)
MU Mitglied
 
Benutzerbild von iScript
 
Registriert seit: 02.2007
Beiträge: 127
so, und hier besagter handler, um daten an bestimmter stelle hinzuzufügen. ich habe ihn genutzt, um daten immer in eine vorformatierte tabelle einzufügen.

Zitat:
on InsertData(SeparatedText, separator, NumberOfStartRow, NumberOfStartCol, TableRef)
   set text item delimiters to separator
   set TextRows to paragraphs of SeparatedText
   set CountTextRows to count TextRows
   set CountTextCol to count text items of item 1 of TextRows
   tell application "Numbers"
      tell TableRef
         
         -- add missing rows
         set CountTableRows to count rows
         set MissingTableRows to ((CountTextRows + (NumberOfStartRow - 1)) - CountTableRows)
         if MissingTableRows > 0 then
            repeat MissingTableRows times
               make new row at end of rows
            end repeat
         end if
         
         -- add missing columns
         set CountTableCol to count columns
         set MissingTableCol to ((CountTextCol + (NumberOfStartCol - 1)) - CountTableCol)
         if MissingTableCol > 0 then
            repeat MissingTableCol times
               make new column at end of columns
            end repeat
         end if
         
         -- rows
         repeat with r from 1 to CountTextRows
            tell row (r + NumberOfStartRow - 1)
               set RowContent to text items of item r of TextRows
               repeat with c from 1 to CountTextCol
                  set value of cell (c + NumberOfStartCol - 1) to item c of RowContent
               end repeat
               
            end tell
         end repeat
      end tell
   end tell
   set text item delimiters to ""
end InsertData
iScript ist offline   Mit Zitat antworten
Alt 11-02-2010, 21:42   #4 (permalink)
MU Mitglied
 
Benutzerbild von AssetBurned
 
Registriert seit: 10.2005
Ort: Warehouse 13
Beiträge: 1.997
warum packst du die nicht einfach in eine csv datei (via textedit oder im terminal mit cat) und importierst dann die csv datei einfach?
AssetBurned ist offline   Mit Zitat antworten
Alt 12-02-2010, 17:44   #5 (permalink)
MU Mitglied
 
Benutzerbild von Babaganoush
 
Registriert seit: 06.2007
Beiträge: 162

Themenautor/in
Hey danke für die ganze Mühe schonmal!!!

Ich habe daraus jetzt folgendes gebastelt:

Code:
set thefiles to choose file with multiple selections allowed
repeat with currentfile in thefiles
	my do_numbers_import(currentfile)
end repeat

on do_numbers_import(Datei)
	tell application "Numbers"
		activate
		tell document (count of documents)
			tell sheet 1
				tell application "Finder"
					set quoteLines to every paragraph of (do shell script "cat " & quoted form of (POSIX path of Datei))
					set filename to (characters 1 through -5 of ((name of Datei) as text) as text)
				end tell
				set rowcount to count of quoteLines
				make new table with properties {name:filename, row count:rowcount, column count:2}
				tell table (count of tables)
					set nRow to 1
					repeat with aLine in quoteLines
						set AppleScript's text item delimiters to ","
						set myvalues to every text item of aLine
						set AppleScript's text item delimiters to ""
						
						repeat with i from 1 to count of myvalues
							set value of cell i of row nRow to ((item i of myvalues) as text)
						end repeat
						set nRow to nRow + 1
					end repeat
				end tell
			end tell
		end tell
	end tell
end do_numbers_import
Weiß noch jemand wie ich dass mit dem Befehl
Code:
set quoteLines to paragraphs of (read Datei from 1 to 530)
koppeln kann? Weil danach in den Dateien Daten stehen die mich nicht interessieren.

Kann ich dass auch so machen, dass alles in eine Tabelle untereinander geschrieben wird? --> Sorry ich weiß leider nicht wie ich den handler einbaue
Babaganoush ist offline   Mit Zitat antworten
Alt 12-02-2010, 21:06   #6 (permalink)
MU Mitglied
 
Benutzerbild von iScript
 
Registriert seit: 02.2007
Beiträge: 127
brauchst du nur diese daten?:

2004-03-01,99
2004-03-02,89
2004-03-03,65
...
iScript ist offline   Mit Zitat antworten
Alt 12-02-2010, 22:21   #7 (permalink)
MU Mitglied
 
Benutzerbild von Babaganoush
 
Registriert seit: 06.2007
Beiträge: 162

Themenautor/in
ja genau
Babaganoush ist offline   Mit Zitat antworten
Alt 13-02-2010, 00:09   #8 (permalink)
MU Mitglied
 
Benutzerbild von AssetBurned
 
Registriert seit: 10.2005
Ort: Warehouse 13
Beiträge: 1.997
irgendwie schwant es mir das nen schnelles bash script einfacher wäre.
AssetBurned ist offline   Mit Zitat antworten
Alt 13-02-2010, 00:45   #9 (permalink)
MU Mitglied
 
Benutzerbild von iScript
 
Registriert seit: 02.2007
Beiträge: 127
so isses. deshalb die frage.

set csvfolder to choose folder with prompt "CSV-Ordner auswählen"
set csvdata to (do shell script "grep -h '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9],*' " & quoted form of POSIX path of csvfolder & "*.csv | sort")

damit haste die daten aller csv im stück.
iScript ist offline   Mit Zitat antworten
Alt 13-02-2010, 15:57   #10 (permalink)
MU Mitglied
 
Benutzerbild von Babaganoush
 
Registriert seit: 06.2007
Beiträge: 162

Themenautor/in
Leider weiß ich aber nicht wie man so etwas mit einem bash script machen würde

Und ich weiß dass ich langsam nerve ( und ich nicht wirklich weiß wie ich mich bedanken soll) aber ich habe jetzt den code "zusammengefügt", aber leider schreibt er mir das noch nicht in numbers rein, wo ist mein Fehler?

Wie muss ich dass schreiben, dass er mir das Datum und den Wert nicht in einer Zelle ausgibt sondern in zwei Spalten?

Schonmal ganz vielen Dank, ich weiß dass man sich besser selbst mit der Materie vertraut machen soll und ich habe mir auch schon ein Applescript Buch gekauft und arbeite mich langsam ein. Nur wie so häufig steht man etwas unvorbereitet vor so einem Problem, habt daher bitte ein wenig nachsicht

Code:
set csvfolder to choose folder with prompt "CSV-Ordner auswählen"
set csvdata to (do shell script "grep -h '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9],*' " & quoted form of POSIX path of csvfolder & "*.csv | sort")
on do_numbers_import(csvdata)
	tell application "Numbers"
		activate
		tell document (count of documents)
			tell sheet 1
				tell application "Finder"
					set quoteLines to every paragraph of (do shell script "cat " & quoted form of (POSIX path of csvdata))
				end tell
				set rowcount to count of quoteLines
				make new table with properties {name:"Daten", row count:rowcount, column count:2}
				tell table (count of tables)
					set nRow to 1
					repeat with aLine in quoteLines
						set AppleScript's text item delimiters to "-"
						set csvdata to every text item of aLine
						set AppleScript's text item delimiters to ""
						
						repeat with i from 1 to count of quoteLines
							set value of cell i of row nRow to ((item i of csvdata) as text)
						end repeat
						set nRow to nRow + 1
					end repeat
				end tell
			end tell
		end tell
	end tell
end do_numbers_import
Babaganoush ist offline   Mit Zitat antworten
Alt 13-02-2010, 21:52   #11 (permalink)
MU Mitglied
 
Benutzerbild von iScript
 
Registriert seit: 02.2007
Beiträge: 127
set csvfolder to choose folder with prompt "CSV-Ordner auswählen"
set csvdata to (do shell script "grep -h '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9],*' " & quoted form of POSIX path of csvfolder & "*.csv | sort")

set quoteLines to paragraphs of csvdata
set row_count to count of quoteLines
set col_count to 2

tell application "Numbers"
   activate
   tell document (count of documents)
      tell sheet 1
         make new table with properties {name:"Daten", row count:row_count, column count:col_count}
         tell table (count of tables)
            set nRow to 1
            repeat with aLine in quoteLines
               set AppleScript's text item delimiters to ","
               set line_data to every text item of aLine
               set AppleScript's text item delimiters to ""
               
               repeat with i from 1 to col_count
                  set value of cell i of row nRow to ((item i of line_data) as text)
               end repeat
               set nRow to nRow + 1
            end repeat
         end tell
      end tell
   end tell
end tell
iScript ist offline   Mit Zitat antworten
Antwort

CSV Dateien per Applescript in Excel/Numbers importieren


Lesezeichen

Themen-Optionen


Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
iCal-Dateien nach Excel/Numbers exportieren krissi123 Mac Os X iApps 1 02-12-2009 18:15
Numbers vs. Excel mato Office Software 9 02-10-2008 17:44
Numbers: Excel-Import? Moondogi Office Software 17 24-05-2008 00:00
Per Applescript Excel Sheets anwählen Britney Spears Applescript und Automator 1 16-03-2007 14:43
CSV Dateien in Excel importieren rob31 Office Software 5 03-05-2006 15:52

Alle Zeitangaben in WEZ +1. Es ist jetzt 19:35 Uhr.


Hilfe? MacUser.de Übersicht


Impressum | Werbung bei MacUser.de
MacUser.de © 2001-2009. All rights reserved

MacUser.de Hosted by
  hosteurope