如何使用Applescript将单个Excel工作表导出为PDF文件?

时间:2016-09-21 21:04:23

标签: excel macos pdf applescript

以下是我正在使用的代码:

saveExcelasPDF("Users:Seth:Documents:OneDrive:Process:12648 0920 Volvo of the Triad 128.xlsx", "Users:Seth:Desktop:test.pdf")

    on saveExcelasPDF(documentPath, PDFName)
        set tFile to (POSIX path of documentPath) as POSIX file
        tell application "/Applications/Microsoft Excel.app"
            set isRun to running
            set wkbk1 to open workbook workbook file name documentPath
            activate object worksheet "Invoice" of wkbk1
            set mySheet to sheet "Invoice" of wkbk1
            alias PDFName -- Necessary to avoid Excel Sandboxing
            save mySheet in PDFName as PDF file format
            close wkbk1 saving no
            if not isRun then quit
        end tell
    end saveExcelasPDF

现在,它将所需书籍中的每张纸张保存到PDF文件中,因此我最终得到的PDF大约有60页。

如何只保存"发票"表到我的PDF文件?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是PDF file format的错误。

当格​​式为save时,CSV file format命令可正常工作。

解决方法是: 将工作表复制到新工作簿。

on saveExcelasPDF(documentPath, PDFName)
    set tFile to (POSIX path of documentPath) as POSIX file
    tell application "/Applications/Microsoft Excel.app"
        set isRun to running
        set wkbk1 to open workbook workbook file name documentPath
        set newWBK to make new workbook
        copy worksheet (sheet "Invoice" of wkbk1) before sheet 1 of newWBK
        close wkbk1 saving no
        alias PDFName -- Necessary to avoid Excel Sandboxing
        save (sheet 1 of newWBK) in PDFName as PDF file format
        close newWBK saving no
        if not isRun then quit
    end tell
end saveExcelasPDF