如何使用AppleScript在Excel中隐藏列?

时间:2017-12-20 16:14:43

标签: excel applescript applescript-excel

我一直在寻找一种通过工作表编写脚本并隐藏特定列的方法。当我研究时,我发现“select column and hide”,但我意识到你必须在运行时首先手动选择列:

tell application "Microsoft Excel"
   set selectedColumn to entire column of cell 1 of selection
   set hidden of selectedColumn to (hidden of selectedColumn)
end tell

所以尝试通过我写的脚本选择列:

tell application "Microsoft Excel"
    set myColumn to range "$A" of sheet 1 of workbook "Workbook1"
    set selectedColumn to entire column of cell myColumn of selection
    set hidden of selectedColumn to (hidden of selectedColumn)
end tell

但它会引发错误:

  

您尝试访问的对象不存在

进一步搜索我发现了Apple“Excel: hide multiple columns with AppleScript”的电子邮件通讯,但当我尝试使用以下内容时:

set hidden of (get entire column of (get range "$A")) to true

但它也会引发:

  

您尝试访问的对象不存在

我发现其他研究“I need a "hide" script for Microsoft Excel”似乎只适用于细胞而非柱。

range我做错了什么?有没有办法在没有击键的情况下隐藏列而不在选择之前或之后识别列?

1 个答案:

答案 0 :(得分:0)

虽然可以使用代码在隐藏之前先选择column,但这不是必需的。

以下示例 AppleScript 代码以{em>号隐藏column

语法: set hidden of column n to true其中n整数,例如:

set hidden of column 1 to true

使用range

执行此操作
set hidden of (entire column of (range "A:A")) to true

以下行为符合active sheet of active workbook

tell application "Microsoft Excel"
    set hidden of (entire column of (range "A:A")) to true
end tell

它相当于:

tell application "Microsoft Excel"
    set hidden of (entire column of (range "A:A")) of active sheet of active workbook to true
end tell

定位特定sheetworkbook使用适当的指示符,例如:

tell application "Microsoft Excel"
    set hidden of (entire column of (range "A:A")) of sheet 1 of workbook 1 to true
end tell

整数值可以是适当的任何值,或者使用name 属性,例如:

tell application "Microsoft Excel"
    set hidden of (entire column of (range "A:A")) of sheet "Sheet1" of workbook "Workbook1" to true
end tell

以下columns隐藏A CErange

tell application "Microsoft Excel"
    set hidden of (entire column of (range "A:A, C:C, E:E")) to true
end tell