【问题标题】:Optimised EmEditor Macro to return Min/Max column lengths on large delimited data优化 EmEditor 宏以返回大型分隔数据的最小/最大列长度
【发布时间】:2020-12-08 18:23:40
【问题描述】:

我目前有大型分隔数据集,我需要返回每列的最小\最大长度。 我目前在 Emeditor v20.3 中使用以下代码,效果很好,但我想知道是否有更快的方法,特别是当有数百万行数据和数百列时(并且此代码很慢)。

非常感谢任何可以包装到 javascript 宏中的更快的方法或想法。


for( col = colStart; col <= MaxCol; col++ ) {
    sTitle = document.GetCell( 1, col, eeCellIncludeNone );
    min = -1;
    max = 0;
    for( line = document.HeadingLines + 1; line < MaxLines; line++ ) {
        str = document.GetCell( line, col, eeCellIncludeQuotesAndDelimiter );
        if( min == -1 || min > str.length ) {
            min = str.length;
        }
        if( max < str.length ) {
            max = str.length;
        }
    }
    OutputBar.writeln( col + min + "    " + max + " " + sTitle);
}

【问题讨论】:

  • 您可以使用 Find Longest Line/Cell 命令和 Find Non-Empty Shortest Line/Cell 命令来优化速度。
  • 太好了。由于最短返回非空,是否有一种最佳方法可以通过宏检查何时/如果最小长度 = 0?
  • 如果最短单元格长度不为零,则可以搜索空单元格。一种方法是在设置 '^' and '$' can Match Beginning and End of the Selection 选项时在所选列中搜索 ^$(正则表达式)。
  • 感谢 Yutaka 的提示,我在想这样的事情,但我会做一些测试。如果在未来的版本中可以添加一个方法来只返回最短(包括零),那就太好了。 var noZeroLengthData = document.Filter("",xOrgCell,eeFindWholeString,0,0,0);
  • 下一个版本 (20.3.906) 将包含 Find Shortest Line/Cell 命令。

标签: performance emeditor


【解决方案1】:

请将 EmEditor 更新到 20.3.906 或更高版本,然后运行此宏:

colStart = 1;
MaxCol = document.GetColumns();
document.selection.EndOfDocument();
yLastLine = document.selection.GetActivePointY( eePosCellLogical );

min = -1;
max = 0;
for( col = colStart; col <= MaxCol; col++ ) {
    sTitle = document.GetCell( 1, col, eeCellIncludeNone );
    document.selection.SetActivePoint( eePosCellLogical, col, 1 );
    
    editor.ExecuteCommandByID( 4064 );  // Find Empty or Shortest Cell
    y = document.selection.GetActivePointY( eePosCellLogical );
    if( y < yLastLine ) {  // check if not the last empty line
        str = document.GetCell( y, col, eeCellIncludeQuotes );
        min = str.length;
    }
    else {  // if the last empty line
        document.selection.SetActivePoint( eePosCellLogical, col, 1 );
        editor.ExecuteCommandByID( 4050 );  // Find Non-empty Shortest Cell
        y = document.selection.GetActivePointY( eePosCellLogical );
        str = document.GetCell( y, col, eeCellIncludeQuotes );
        min = str.length;
    }
    
    document.selection.SetActivePoint( eePosCellLogical, col, 1 );
    editor.ExecuteCommandByID( 4049 );  // Find Longest Cell
    y = document.selection.GetActivePointY( eePosCellLogical );
    str = document.GetCell( y, col, eeCellIncludeQuotes );
    max = str.length;
    OutputBar.writeln( col + " : " + min + "    " + max + " " + sTitle);
}

【讨论】:

  • 优秀 - 按预期工作并且速度更快(希望在未来的版本中,它可能能够运行得更快)。谢谢你裕!
猜你喜欢
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-15
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
相关资源
最近更新 更多