+ Auf Thema antworten
Zeige Ergebnis 1 bis 11 von 11

Thema: CSV Dateien per Applescript in Excel/Numbers importieren

  1. #1
    MU Mitglied Benutzerbild von Babaganoush
    Registriert
    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
    Keiner ist so verrückt, daß er nicht einen noch Verrückteren findet, der ihn versteht
    F. Nietzsche

  2. #2
     iScript ist offline
    MU Mitglied Benutzerbild von iScript
    Registriert
    02.2007
    Beiträge
    139
    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
            &# 160;set AppleScript's text item delimiters to ","
            &# 160;make new table with properties {name:"Daten", row count:count quoteLines, column count:count text items of item 1 of quoteLines}
            &# 160;tell table (count of tables)
            &# 160;   set nRow to 1
            &# 160;   repeat with aLine in quoteLines
            &# 160;      set cellValues to every text item of aLine
            &# 160;      set nCol to 1
            &# 160;      repeat with aCell in cellValues
            &# 160;       &#16 0; set value of cell nCol of row nRow to aCell
            &# 160;       &#16 0; set nCol to nCol + 1
            &# 160;      end repeat
            &# 160;      set nRow to nRow + 1
            &# 160;   end repeat
            &# 160;end tell
            &# 160;set AppleScript's text item delimiters to ""
          end tell
       end tell
    end tell
    Geändert von iScript (11-02-2010 um 00:09 Uhr)

  3. #3
     iScript ist offline
    MU Mitglied Benutzerbild von iScript
    Registriert
    02.2007
    Beiträge
    139
    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.

    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
            &# 160;
            &# 160;-- add missing rows
            &# 160;set CountTableRows to count rows
            &# 160;set MissingTableRows to ((CountTextRows + (NumberOfStartRow - 1)) - CountTableRows)
            &# 160;if MissingTableRows > 0 then
            &# 160;   repeat MissingTableRows times
            &# 160;      make new row at end of rows
            &# 160;   end repeat
            &# 160;end if
            &# 160;
            &# 160;-- add missing columns
            &# 160;set CountTableCol to count columns
            &# 160;set MissingTableCol to ((CountTextCol + (NumberOfStartCol - 1)) - CountTableCol)
            &# 160;if MissingTableCol > 0 then
            &# 160;   repeat MissingTableCol times
            &# 160;      make new column at end of columns
            &# 160;   end repeat
            &# 160;end if
            &# 160;
            &# 160;-- rows
            &# 160;repeat with r from 1 to CountTextRows
            &# 160;   tell row (r + NumberOfStartRow - 1)
            &# 160;      set RowContent to text items of item r of TextRows
            &# 160;      repeat with c from 1 to CountTextCol
            &# 160;       &#16 0; set value of cell (c + NumberOfStartCol - 1) to item c of RowContent
            &# 160;      end repeat
            &# 160;      
            &# 160;   end tell
            &# 160;end repeat
          end tell
       end tell
       set text item delimiters to ""
    end InsertData

  4. #4
    MU Mitglied Benutzerbild von AssetBurned
    Registriert
    10.2005
    Ort
    Auf der Insel mit der guten Butter.
    Beiträge
    2.037
    warum packst du die nicht einfach in eine csv datei (via textedit oder im terminal mit cat) und importierst dann die csv datei einfach?
    Das Posting gibt eine private Meinung wieder und kann, nicht zwingend als solche gekennzeichnete, Ironie beinhalten.
    MB2,1 2GHz 320/4GB/SLeo & Mini3,1 2.26 500/4/Leo,Touch2G16,DROBO,APC BK800i

  5. #5
    MU Mitglied Benutzerbild von Babaganoush
    Registriert
    06.2007
    Beiträge
    162
    Themenstarter
    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
    Keiner ist so verrückt, daß er nicht einen noch Verrückteren findet, der ihn versteht
    F. Nietzsche

  6. #6
     iScript ist offline
    MU Mitglied Benutzerbild von iScript
    Registriert
    02.2007
    Beiträge
    139
    brauchst du nur diese daten?:

    2004-03-01,99
    2004-03-02,89
    2004-03-03,65
    ...

  7. #7
    MU Mitglied Benutzerbild von Babaganoush
    Registriert
    06.2007
    Beiträge
    162
    Themenstarter
    ja genau
    Keiner ist so verrückt, daß er nicht einen noch Verrückteren findet, der ihn versteht
    F. Nietzsche

  8. #8
    MU Mitglied Benutzerbild von AssetBurned
    Registriert
    10.2005
    Ort
    Auf der Insel mit der guten Butter.
    Beiträge
    2.037
    irgendwie schwant es mir das nen schnelles bash script einfacher wäre.
    Das Posting gibt eine private Meinung wieder und kann, nicht zwingend als solche gekennzeichnete, Ironie beinhalten.
    MB2,1 2GHz 320/4GB/SLeo & Mini3,1 2.26 500/4/Leo,Touch2G16,DROBO,APC BK800i

  9. #9
     iScript ist offline
    MU Mitglied Benutzerbild von iScript
    Registriert
    02.2007
    Beiträge
    139
    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.

  10. #10
    MU Mitglied Benutzerbild von Babaganoush
    Registriert
    06.2007
    Beiträge
    162
    Themenstarter
    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
    
    Keiner ist so verrückt, daß er nicht einen noch Verrückteren findet, der ihn versteht
    F. Nietzsche

  11. #11
     iScript ist offline
    MU Mitglied Benutzerbild von iScript
    Registriert
    02.2007
    Beiträge
    139
    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
            &# 160;make new table with properties {name:"Daten", row count:row_count, column count:col_count}
            &# 160;tell table (count of tables)
            &# 160;   set nRow to 1
            &# 160;   repeat with aLine in quoteLines
            &# 160;      set AppleScript's text item delimiters to ","
            &# 160;      set line_data to every text item of aLine
            &# 160;      set AppleScript's text item delimiters to ""
            &# 160;      
            &# 160;      repeat with i from 1 to col_count
            &# 160;       &#16 0; set value of cell i of row nRow to ((item i of line_data) as text)
            &# 160;      end repeat
            &# 160;      set nRow to nRow + 1
            &# 160;   end repeat
            &# 160;end tell
          end tell
       end tell
    end tell

+ Auf Thema antworten

Forumregeln

  • Es ist dir nicht erlaubt, neue Themen zu verfassen.
  • Es ist dir nicht erlaubt, auf Beiträge zu antworten.
  • Es ist dir nicht erlaubt, Anhänge hochzuladen.
  • Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.