【问题标题】:How to find and replace multiple numbers with JavaScript with an UltraEdit script?如何使用 UltraEdit 脚本用 JavaScript 查找和替换多个数字?
【发布时间】:2021-07-05 03:58:49
【问题描述】:

我有file.txt 的测量报告,其中包含许多测量点。现场测量点具有临时测量点。我现在想更换所有临时测量点。

如何自动多次使用函数Find & Replace,具有固定的现场测量点列表和真实测量点列表?

例如:

旧号(临时测点):10001、10002、10003
新数字(实测点):251004、251005、251006

我的测量点太多,无法手动完成。

是否有可能拥有一个使用 JavaScript 核心作为解释器的 UltraEdit 脚本,它可以在 UltraEdit 中运行以非常快速地执行此操作?或者文本编辑器还有其他方法吗?

每次实际测量点的列表都会有所不同。所以我只想更新脚本中的真实测量点列表并再次运行。

【问题讨论】:

    标签: javascript replace find


    【解决方案1】:

    这个任务可以用文本编辑器 UltraEdit 和下面的 UltraEdit 脚本来完成:

    var asOldNumbers = ["10001","10002","10003"];
    var asNewNumbers = ["251004","251005","251006"];
    
    if (asOldNumbers.length == asNewNumbers.length)
    {
        if (UltraEdit.document.length > 0)  // Is any file opened?
        {
            // Define environment for this script.
            UltraEdit.insertMode();
            UltraEdit.columnModeOff();
    
            // Move caret to top of the active file.
            UltraEdit.activeDocument.top();
    
            // Define the parameters for the case-sensitive replaces matching whole
            // word only executed in a loop for each pair of old and new number.
            UltraEdit.ueReOn();
            UltraEdit.activeDocument.findReplace.mode=0;
            UltraEdit.activeDocument.findReplace.matchCase=true;
            UltraEdit.activeDocument.findReplace.matchWord=true;
            UltraEdit.activeDocument.findReplace.regExp=false;
            UltraEdit.activeDocument.findReplace.searchDown=true;
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
            UltraEdit.activeDocument.findReplace.preserveCase=false;
            UltraEdit.activeDocument.findReplace.replaceAll=true;
            UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
    
            // Run the replace all command for each pair of old and new number.
            for (var nNumberIndex = 0; nNumberIndex < asOldNumbers.length; nNumberIndex++)
            {
                UltraEdit.activeDocument.findReplace.replace(asOldNumbers[nNumberIndex],asNewNumbers[nNumberIndex]);
            }
        }
        else UltraEdit.messageBox("There is no file opened!","SCRIPT EXECUTION ERROR");
    }
    else UltraEdit.messageBox("The number of old numbers is not equal the number of new numbers!","SCRIPT CODING ERROR");
    

    新旧数字必须在脚本顶部定义为两个字符串数组的元素。

    首先从文件中读取实际测量点(新数字)也很容易,而不是在脚本中进行硬编码,然后运行替换。甚至可以在测量报告的模板文件上完全自动化此任务,并且具有来自上次现场测量的真实测量点的数据文件具有固定的文件名和存储位置。然后可以使用快捷文件(.lnk 文件)或批处理文件来运行 UltraEdit 打开模板和数据文件,在模板文件中进行替换,并通过脚本以新名称保存修改后的报告文件,最后退出 UltraEdit。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 2017-03-10
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多