Applescript: Automatisieren Sie Excel, um .xls in .csv zu konvertieren

Ich versuche, einen Ordner mit .xls-Dateien mit Applescript für MS Excel V15.15 im Stapelbetrieb in .csv zu konvertieren. Ich verwende ein Beispiel, das ich online gefunden habe, um es zu modellieren:

set theOutputPath to (path to desktop folder as string) & "My Saved Workbook.csv"
  tell application "Microsoft Excel"
    tell active workbook
      save workbook as filename theOutputPath file format CSV file format
    end tell
  end tell

Dies ist das Skript, das ständig fehlschlägt, obwohl es dem Modell am ähnlichsten zu sein scheint:

set csv_folder to "Macintosh HD:Users:Me:CSV:" & file_name as string
    tell application "Microsoft Excel"
        open Source_file
        tell active workbook
            save workbook as filename csv_folder file format CSV Mac file format-->
           (*This generates error "Microsoft Excel got an error: Parameter error." number -50 *)
        end tell
    end tell

Ich habe auch versucht:

set csv_folder to "Macintosh HD:Users:Me:CSV:" & file_name & ".csv" as string

tell application "Microsoft Excel"
    open Source_file
        tell active workbook
            save workbook as filename csv_folder -->
           (*This usually generates error "Microsoft Excel got an error:
            Parameter error." number -50 the first time it is run, 
              then works the 2nd time *)
        end tell
end tell

BEARBEITEN: Dieses letztere Skript führt, obwohl es abgeschlossen ist, nicht zu einer echten CSV-Datei, da es beim Öffnen mit BBEdit Code und nicht den Dateiinhalt anzeigt.

Ich habe auch versucht, cv_folder einzustellen, ohne "as String" zu verwenden. Irgendwelche Ideen, warum dies fehlschlägt? Es scheint die Syntax "Speichern Sie die Arbeitsmappe als Dateiname im Ausgabepfad-Dateiformat CSV-Dateiformat" nicht zu mögen.

Antworten (2)

Verwenden Sie für Excel V15 einen Posix-Pfad wie diesen:

set theOutputPath to POSIX path of ((path to desktop folder as string) & "My Saved Workbook.csv")
tell application "Microsoft Excel"
    tell active workbook
        save workbook as filename theOutputPath file format CSV Mac file format with overwrite
    end tell
end tell

Bearbeiten 1 -

Ich teste es auf V15.15 und V15.16 , es ist ein Fehler, wenn der Zielordner keine kürzlich geöffnete Excel-Datei enthält. Also benutze es

set theOutputPath to POSIX path of ((path to desktop folder as string) & "My Saved Workbook.csv")
set parentFolder to (do shell script "dirname " & quoted form of theOutputPath) as POSIX file -- get the parent folder 

tell application "Microsoft Excel"
    alias parentFolder -- a folder where to save a new file, workaround to a bug when the destination folder doesn't contains a recently opened Excel file
    save workbook as active workbook filename (theOutputPath) file format CSV Mac file format with overwrite
end tell

Bearbeiten 2 -

Oder erstellen Sie eine leere Datei wie diese

set theOutputPath to POSIX path of (path to desktop folder as string) & "My Saved Workbook.csv"
do shell script "touch " & quoted form of theOutputPath -- create an empty file
set theOutputPath to theOutputPath as POSIX file
tell application "Microsoft Excel"
    save workbook as active workbook filename (theOutputPath) file format CSV Mac file format with overwrite
end tell

Bearbeiten 3 : Ich hatte eine andere Idee, es wäre das einfachste Skript.

set theOutputPath to (path to desktop folder as string) & "My Saved Workbook.csv"
tell application "Microsoft Excel"
    alias theOutputPath --  workaround to a bug when the destination folder doesn't contains a recently opened Excel file
    save workbook as active workbook filename theOutputPath file format CSV Mac file format with overwrite
end tell
Danke für die Hilfe. Das von Ihnen bereitgestellte Skript gibt immer noch denselben Parameterfehler für die Zeile "Arbeitsmappe speichern als Dateiname im Ausgabepfad-Dateiformat CSV-Mac-Dateiformat mit Überschreiben" zurück. Ich beginne zu glauben, dass dies eine Änderung in Excel für Mac v15 ist, da keine der Syntax für die Optionen „Speichern unter [Dateityp]“, die ich in Excel 15 gefunden habe, funktioniert.
Ich habe meine Antwort aktualisiert.

Es scheint, dass der Parameterfehler auf Änderungen mit Office 2016 für Mac zurückzuführen ist.

http://preview.alturl.com/f8srb

Nach dem, was ich aus diesem Thread gelernt habe, kann Excel V15 nur automatisch (ausgelöst durch AppleScript) innerhalb des Pfads ~/Library/Containers/com.microsoft.excel/ schreiben. Es sieht so aus, als müsste ich entweder zur vorherigen Version von Excel zurückkehren oder die Dateien in diesen Ordner schreiben und dann an den gewünschten Ort kopieren. Was für ein PITA.