CSV Dateien per Applescript in Excel/Numbers importieren

Babaganoush

Babaganoush

Mitglied
Thread Starter
Dabei seit
19.06.2007
Beiträge
175
Reaktionspunkte
2
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 :)
 
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
 
Zuletzt bearbeitet:
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
         
         -- 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
 
warum packst du die nicht einfach in eine csv datei (via textedit oder im terminal mit cat) und importierst dann die csv datei einfach?
 
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 :(
 
brauchst du nur diese daten?:

2004-03-01,99
2004-03-02,89
2004-03-03,65
...
 
irgendwie schwant es mir das nen schnelles bash script einfacher wäre.
 
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.
 
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
 
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
 
Zurück
Oben Unten