2015-06-04 31 views
0

第一篇文章所以这里去...问题保存Excel工作簿中的AppleScript

我想写一个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 

我做错了什么?我需要对这一点进行排序,然后才能继续完成工作簿并将其作为附件放入电子邮件中。

在此先感谢

杰斯

+0

我在下面添加了一个答案。如果它适合你,那么将答案标记为解决方案,否则提供更多的反馈,如果它没有做到这一点。 –

回答

0

Jez的M-L

下张复印纸用的优胜美地和Excel 2011的Mac本地采样工作过。

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 

这可能是适合回你的逻辑有用:你认为合适,您可以用替换对话框或任何硬编码的文件名(wkbkSrcName和wkbkTargName)。

相关问题