【问题标题】:Issue saving a excel workbook in applescript在applescript中保存excel工作簿的问题
【发布时间】:2015-08-18 16:26:54
【问题描述】:

第一篇文章就到这里了……

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

我对 applescript 比较陌生,但我已经设法使用 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

我做错了什么?我需要先整理一下,然后才能继续整理工作簿并将其作为附件放入电子邮件中。

提前致谢

杰斯

【问题讨论】:

  • 我在下面添加了一个答案。如果它对您有用,则将答案标记为解决方案,否则如果它没有解决问题,请提供更多反馈。

标签: excel macos applescript


【解决方案1】:

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

这可能有助于重新适应您的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多