Applescritp Excel查找和替换“

时间:2016-09-14 16:57:06

标签: excel replace find applescript

我需要一个AppleScript才能找到“并且只用空格替换。我在这里发现了一个很棒的脚本,可以很好地找到并替换整个单词(你好,世界)但是当你操纵脚本找到”并替换为一个空间,“破坏代码,它不再有效。我希望有人知道一种方法来改变这个代码,使它做我想做的或有另一个想法。这是代码(代码信用到adamh):

searchAndReplaceTextInCells("hello", "world")

on searchAndReplaceTextInCells(search_str, replace_str)

tell application "Microsoft Excel"

    set search_range to range "A:Z"
    set all_found_ranges to {} -- store for the ranges, to manipulate after searching
    set found_range to ""
    set counter to 0
    try
        set found_range to find search_range what search_str with match case
    on error
        log ("No matches found")
    end try

    if (found_range is not "") then
        set first_cell_address to (get address of the cells of found_range) -- we use this to break our loop
        repeat while true
            set counter to counter + 1
            copy found_range to end of all_found_ranges

            -- Now look for next result
            set found_range to find next search_range after found_range
            set cell_address to (get address of the cells of found_range)

            if (cell_address = first_cell_address) then 
                -- have looped around so we are finished!
                exit repeat
            end if
        end repeat

    end if

    -- walk all the ranges found and do the string replacing
    repeat with r in all_found_ranges
        set value of r to my replace_chars(the value of r, search_str, replace_str)
    end repeat

    log ("found and replaced " & counter & " items")
end tell
end searchAndReplaceTextInCells

on replace_chars(this_text, search_string, replacement_string)
set my text item delimiters to the search_string
set the item_list to every text item of this_text
set my text item delimiters to the replacement_string
set this_text to the item_list as string
set my text item delimiters to ""
return this_text
end replace_chars

1 个答案:

答案 0 :(得分:0)

由于" 是保留字符,因此在以字符串形式引用时需要对其进行区别对待。

您可以使用quote常量作为参数:

searchAndReplaceTextInCells(quote, " ")

..或者您可以将其作为转义字符发送:

searchAndReplaceTextInCells("\"", " ")