【问题标题】:Find and replace in Excel 2011 using Applescript使用 Applescript 在 Excel 2011 中查找和替换
【发布时间】:2013-12-17 12:01:04
【问题描述】:

我想知道是否有人可以帮助我。我正在尝试在 Excel 工作簿中查找和替换空格。我希望它搜索我在工作簿中的当前工作表,找到“”的所有实例并将其替换为“”。

有人碰巧知道怎么做吗?

【问题讨论】:

    标签: macos excel replace applescript


    【解决方案1】:

    使用 Applescript:

    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
    

    【讨论】:

      【解决方案2】:

      Command + F 怎么样,然后点击替换按钮给你这个:

      【讨论】:

      • 那太好了,但我问的是如何使用 Applescript 来实现,正如我的帖子标题所暗示的那样。
      【解决方案3】:

      您是否尝试过以下方法:

      替换范围targetRange什么searchStr替换replaceStr

      这应该是相当直接的替换语句。

      【讨论】:

        猜你喜欢
        • 2017-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        相关资源
        最近更新 更多