我正在尝试删除具有特定值的 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
大多数代码并不重要,包括 fullRange 变量和设置在顶部的路径变量。问题是删除代码在第 2 行之后甚至没有继续进行,并且在此之上运行非常缓慢,因此实际上存在很多问题。我猜我做的那个循环一定有问题。如果有人能让我走上正确的轨道,那就太好了。
经过大量的试验/挖掘,我最近发现了这个问题。当我删除范围时,行实际上移动了前导 excel 以删除错误的范围。我目前正在研究一种解决方法,包括清除我不想要的行并隐藏所有空白单元格,然后将所有可见单元格复制到新工作表中。
好的,另一个更新,最后一次更新。我终于完成了脚本,让它可以做它需要做的事情。而不是所有的清除、隐藏和复制,我只是通过将所有要删除的行添加到一个数组中来完成删除工作,而不是将数组转换为一个范围字符串,删除范围可以一次性删除所有行。这是一种更快、更简单的解决方案。
这是伟大的苹果脚本:
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
我希望这对那里的人有所帮助!我有解决自己问题的坏习惯。:)。