【问题标题】:How to use applescript to save a single Excel sheet as a PDF如何使用applescript将单个Excel工作表另存为PDF
【发布时间】:2017-03-21 10:56:18
【问题描述】:

我正在使用以下代码:

set PDFName to --desired path and name of PDF file to save
set excelName to --desired path and name of Excel file to open

tell application "/Applications/Microsoft Excel.app"
    set isRun to running
    set wkbk1 to open workbook workbook file name excelName

    --Problem
    save (sheet "Invoice" of wkbk1) in PDFName as PDF file format
    --/Problem

    close wkbk1 saving no
    if not isRun then quit
end tell

我最终将整个工作簿转换为 60 页长的 PDF,而不仅仅是一张纸!难道我做错了什么?我该如何解决这个问题?

【问题讨论】:

  • 我认为您的答案在 Stackoverflow 链接中:link
  • 我实际上是用那篇文章来写这段代码的。我遇到的问题与我认为他遇到的问题不同......
  • 我自己测试过,尽管“保存活动工作表”,它保存了 Excel 2011 中的所有工作表。这似乎是 Excel 错误!然后我建议在保存之前删除所有不想转换的工作表......然后只有你想要的工作表会被转换。 ...并关闭工作簿而不保存。
  • 是的,我实际上已经尝试过了。问题是这张表上的很多数据都是从其他表中提取的......所以一旦我删除了其他表,这些数据就会随之而来,我没有可用于转换为 PDF...

标签: excel applescript pdf-generation


【解决方案1】:

我尝试仅在新工作表中复制/粘贴值,但失败了。特殊的粘贴……什么都没有。

因此,我提取了第一个主文档的工作表并将其复制到一个新的临时文档中,然后我将临时文档另存为 PDF 并关闭新的临时文档和主文档。

它适用于下面的脚本:

set PDFPath to "Users:imac27:Desktop:Test22.PDF" -- destination of the PDF

tell application "Microsoft Excel"
activate
set SourceDoc to front document
set NameWB to name of front document -- get name of the document (for close)
tell SourceDoc to set SourceSheet to active sheet -- get sheet to be extracted
set newDoc to make new workbook -- create new workbook
tell newDoc
    set FirstSheet to first sheet of newDoc
    copy worksheet SourceSheet after FirstSheet -- copy sheet to be extracted in new doc
end tell
set display alerts to false -- to avoid warning "are you sure...?)
delete FirstSheet -- delete defautl sheet of new doc to keep only the copied sheet
save active sheet in PDFPath as PDF file format -- save temp workbook as PDf
close active workbook saving no -- close temp workbook
close workbook NameWB saving no -- close main document
end tell

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2016-09-20
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多