在AppleScript中保存excel工作簿的问题

时间:2015-06-04 07:17:36

标签: excel macos applescript applescript-excel

第一篇文章,所以这里......

我正在尝试编写一个AppleScript来将现有工作簿中的工作表复制到新工作簿中,将单个工作表保存到新工作簿中,然后将该工作簿附加到电子邮件中。

我对applecript相对较新,但我已设法使用Cocoadialog进行用户交互,脚本将创建新工作簿,然后复制工作表,但这就是问题的起点。

我正好创建新工作簿,然后将该工作簿的名称设置为变量以便稍后调用。然后我复制了之前设置的var表单,并将表单复制到看起来像是一个新的工作簿,所以当我保存工作簿时,它会使用正确的文件名保存在正确的位置但是它是空白的,并且有一个表单被调用sheet1在名为sheet1的工作簿中。足够的解释,这是代码......

set choice to 1
set tabnames to ""

repeat until (choice = 2)
     --cocoadialog dropdownbox to get target excel workbook
     set filechoice to paragraphs of (do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog" & " fileselect --title 'This is another fileselect' --text 'Pick some files andor directories' --with-directory $HOME/Dropbox/Business/Current/")
    tell application "Microsoft Excel"
        open filechoice
        set sourceWorkbook to workbook (get name of active workbook)

        set noOfTabs to count of worksheets of active workbook --get numer of tabs in target spreadsheet

        repeat with x from 1 to noOfTabs --get all tab names
            set tabnames to the name of every sheet
        end repeat

        set selectedTabName to (choose from list tabnames) as string --get user to choose which tab to copy

        set destWorkbook to make new workbook --create new workbook
        set destWorkbook to workbook (get name of active workbook)
        tell destWorkbook --get count of tabs
            set lastsheet to sheet (count of sheets)
        end tell
        tell sourceWorkbook
            set sourcesheet to sheet selectedTabName
            copy worksheet sourcesheet after lastsheet of workbook destWorkbook --copy sourcesheet to new workbook
        end tell

        tell destWorkbook

            --get save path and add filename
            set selectedPath to paragraphs of (do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog" & " fileselect --title 'Select Folder to save to' --text  --with-directory $HOME/Dropbox/Business/Timesheets/2015/ --select-only-directories") & "/JML Timesheet " & selectedTabName as string

            set destWorkbookName to (POSIX file selectedPath) as string 

            save workbook as destWorkbook filename destWorkbookName file format workbook normal file format overwrite yes --save new workbook 

        end tell
    end tell
    set choice to 2
end repeat

我做错了什么?在我继续收尾并将工作簿作为附件放入电子邮件之前,我需要对此进行排序。

提前致谢

Jez的

1 个答案:

答案 0 :(得分:0)

Jez M-L

以下复印表使用Yosemite和Excel 2011 for Mac在本地样本中工作。您可以使用对话框或其他任何内容替换硬编码的文件名(wkbkSrcName和wkbkTargName):

set wkbkPath to (POSIX path of (run script "path to desktop")) as text
set wkbkSrcName to "clasew-ex5-sample1a.xlsx"
set wkbkTargName to "sample-target.xlsx"

tell application "Microsoft Excel"
    launch
    local fullSrcFile, fullTargFile, srcSheet, lastTargSheet, wkbkSrc, wkbkTarg

    set fullSrcFile to (wkbkPath & wkbkSrcName)
    open fullSrcFile
    set wkbkSrc to workbook wkbkSrcName
    set srcSheet to sheet 1 of wkbkSrc

    set wkbkTarg to make new workbook
    set lastTargSheet to sheet (count of sheets) of wkbkTarg

    copy worksheet srcSheet after lastTargSheet

    tell wkbkTarg
        set fullTargFile to (POSIX file wkbkPath & wkbkTargName) as text
        save workbook as filename fullTargFile overwrite yes
    end tell

end tell

这可能有助于回到你的逻辑中。