编写用于删除单元格中具有特定值的行的Apple脚本

我想删除Excel中的单元格与一定的值。 我已经走得很远,已经写了我自己的剧本借用一些互联网摘录。

set theOutputPath to (path to desktop folder as string) & "FilteredNY.csv" set fstring to "(Not NY State)" tell application "Microsoft Excel" activate open workbook workbook yatta yatta activate object worksheet 1 -->activate range current region set LastRow to ((count of rows of used range of active sheet) + 1) set lastCol to count of columns of used range of active sheet set lastCol to (get address of column lastCol without column absolute) set lastCol to (characters 1 thru ((length of lastCol) div 2) of lastCol as string) set fullRange to range ("2:" & LastRow) repeat with i from 2 to LastRow --code in repeat if value of cell ("J" & i) is equal to fstring then delete range row i end if end repeat end tell 

大部分代码并不重要,包括fullRangevariables和顶部设置的pathvariables。 问题是删除代码甚至没有进行第2行之后,它运行得非常慢,所以它实际上有很多问题。 我猜想我所做的那个循环肯定有什么问题。 如果任何人都能把我放在正确的轨道上,这将是伟大的。

实际上,我最近经过大量的实验/挖掘发现了这个问题。 当我删除范围的行实际上移动领先的Excel删除错误的范围。 我目前正在处理一个解决方法,包括只清除我不想要的行并隐藏所有空白单元格,然后将所有可见单元格复制到一个新工作表。

好的另一个更新,最后更新。 我终于完成了脚本,允许它做它需要做的事情。 而不是所有的清除,隐藏和复制,我只是通过添加数组中要删除的所有行来完成删除工作,而不是将数组转换为范围string,删除范围将一次删除所有行。 这是一个更快,更简单的解决scheme。

这是伟大的苹果脚本:

  set theOutputPath to (path to desktop folder as string) & "FilteredNY.csv" set fstring to "(Not NY State)" set del_list to {} tell application "Microsoft Excel" activate open workbook workbook file name yatta yatta activate object worksheet 1 set LastRow to ((count of rows of used range of active sheet)) repeat with i from 2 to LastRow --code in repeat if value of cell ("J" & i) is equal to fstring then set di to i as string set end of del_list to di & ":" & di end if end repeat set AppleScript's text item delimiters to "," set ToDelete to del_list as string set AppleScript's text item delimiters to {""} delete range range ToDelete end tell 

我希望这有助于那里的人! 我有一个解决自己的问题的坏习惯。 :)。

在使用带有applescript的Excel时,尽量避免循环,并使用Excel强度在很短的时间内处理大量的行/列。

对于你的情况,我明白你想要删除列“J”的值为“(不是纽约州)”的所有行“你不碰第一行必须有你的列标题。

所以这里是代码:

 tell application "Microsoft Excel" set autofilter mode of active sheet to false set lastRow to first row index of last row of used range autofilter range row ("1:" & lastRow) field 10 criteria1 "(Not NY State)" select row ("2:" & lastRow) delete selection -- if you want to delete #clear contents selection -- if you only wish to clear the data end tell 

这不是更快吗?

奖金技巧来加速您的代码:

 tell application "Microsoft Excel" to set screen updating to false --vvv--- -- insert here operations that would change Excel display -- tell application "Microsoft Excel" to set screen updating to true --^^^--- 

你也可以使用自动筛选器+sorting

但真正重要的是在Excel中巧妙地使用公式来快速完成工作。 (设置一个单元格的公式,然后填写到最后一行)