优化的 EmEditor 宏以转置和转换制表符分隔的数据
Optimised EmEditor Macro to transpose and transform tab delimited data
我目前有一个很大的制表符分隔数据集,看起来像这样(n 只是一个数字,有 4 个以上 Headers),一个重复的初始 Headers 列,然后是最多2列数据(有时只有一列):
输入文件:
HDR1 A1 B1
Hdr2 A2 B2
Hdr3 A3 B3
Hdrn安营
Hdr1 C1
Hdr2 C2
Hdr3 C3
Hdrn Cn
Hdr1 D1 E1
Hdr2 D2 E2
Hdr2 D3 E3
Hdrn Dn En
我需要转置和转换数据,使输出看起来与此类似(所以重复的 headers 被删除,数据保留):
Hdr1 Hdr2 Hdr3 Hdrn
A1 A2 A3 安
B1 B2 B3 Bn
C1 C2 C3 Cn
D1 D2 D3 Dn
E1 E2 E3 恩
任何有关如何使用优化的 EmEditor javascript 宏执行此操作的想法将不胜感激。
这里有一个 JavaScript 宏供您使用:
sSeparator = "Hdr1";
sDelimiter = "\t";
sNL = "\r\n";
Redraw = false;
document.CellMode = true; // Must be cell selection mode
editor.ExecuteCommandByID(4323); // Clear All Bookmarks in This Document
document.Filter("^" + sSeparator,0,eeFindReplaceCase | eeFindReplaceRegExp,0,0,0);
editor.ExecuteCommandByID(3927); // Bookmark All
document.Filter("",0,0,0,0,0); // Reset Filter
if( document.BookmarkCount == 0 ) {
alert( "Cannot find any lines that begin with \"" + sSeparator + "\"" );
Quit();
}
document.selection.StartOfDocument(false);
x1 = 1;
y1 = 1;
nLines = 0;
nMaxCol = document.GetColumns();
str = "";
for( ;; ) {
bStop = false;
if( document.selection.NextBookmark() ) { // if next bookmark found
y2 = document.selection.GetActivePointY(eePosCellLogical);
}
else {
y2 = document.GetLines(); // if bookmark does NOT exist at end of document
bStop = true;
}
if( nLines == 0 ) {
nLines = y2 - y1;
}
else {
if( nLines != y2 - y1 ) {
alert( "Number of lines between " + sSeparator + " is not same. Check the format of the input file." );
Quit();
}
}
for( iCol = x1; iCol <= nMaxCol; ++iCol ) {
s = document.GetCell( y1, iCol, eeCellIncludeQuotes );
if( s.length == 0 ) {
break;
}
str += s;
str += sDelimiter;
str += document.GetColumn( iCol, sDelimiter, eeCellIncludeQuotes, y1 + 1, y2 - y1 - 1 );
str += sNL;
}
y1 = y2;
if( bStop ) {
break;
}
x1 = 2; // don't need the first column except the first time
}
editor.NewFile();
document.selection.Text = str; // insert copied text
editor.ExecuteCommandByID(22529); // set TSV mode
到运行这个,保存这个代码,例如Transpose.jsee
,然后select这个文件从Select... 在 宏 菜单中。最后,select 运行 Transpose.jsee 在 Macros 菜单中。
我目前有一个很大的制表符分隔数据集,看起来像这样(n 只是一个数字,有 4 个以上 Headers),一个重复的初始 Headers 列,然后是最多2列数据(有时只有一列):
输入文件:
HDR1 A1 B1
Hdr2 A2 B2
Hdr3 A3 B3
Hdrn安营
Hdr1 C1
Hdr2 C2
Hdr3 C3
Hdrn Cn
Hdr1 D1 E1
Hdr2 D2 E2
Hdr2 D3 E3
Hdrn Dn En
我需要转置和转换数据,使输出看起来与此类似(所以重复的 headers 被删除,数据保留):
Hdr1 Hdr2 Hdr3 Hdrn
A1 A2 A3 安
B1 B2 B3 Bn
C1 C2 C3 Cn
D1 D2 D3 Dn
E1 E2 E3 恩
任何有关如何使用优化的 EmEditor javascript 宏执行此操作的想法将不胜感激。
这里有一个 JavaScript 宏供您使用:
sSeparator = "Hdr1";
sDelimiter = "\t";
sNL = "\r\n";
Redraw = false;
document.CellMode = true; // Must be cell selection mode
editor.ExecuteCommandByID(4323); // Clear All Bookmarks in This Document
document.Filter("^" + sSeparator,0,eeFindReplaceCase | eeFindReplaceRegExp,0,0,0);
editor.ExecuteCommandByID(3927); // Bookmark All
document.Filter("",0,0,0,0,0); // Reset Filter
if( document.BookmarkCount == 0 ) {
alert( "Cannot find any lines that begin with \"" + sSeparator + "\"" );
Quit();
}
document.selection.StartOfDocument(false);
x1 = 1;
y1 = 1;
nLines = 0;
nMaxCol = document.GetColumns();
str = "";
for( ;; ) {
bStop = false;
if( document.selection.NextBookmark() ) { // if next bookmark found
y2 = document.selection.GetActivePointY(eePosCellLogical);
}
else {
y2 = document.GetLines(); // if bookmark does NOT exist at end of document
bStop = true;
}
if( nLines == 0 ) {
nLines = y2 - y1;
}
else {
if( nLines != y2 - y1 ) {
alert( "Number of lines between " + sSeparator + " is not same. Check the format of the input file." );
Quit();
}
}
for( iCol = x1; iCol <= nMaxCol; ++iCol ) {
s = document.GetCell( y1, iCol, eeCellIncludeQuotes );
if( s.length == 0 ) {
break;
}
str += s;
str += sDelimiter;
str += document.GetColumn( iCol, sDelimiter, eeCellIncludeQuotes, y1 + 1, y2 - y1 - 1 );
str += sNL;
}
y1 = y2;
if( bStop ) {
break;
}
x1 = 2; // don't need the first column except the first time
}
editor.NewFile();
document.selection.Text = str; // insert copied text
editor.ExecuteCommandByID(22529); // set TSV mode
到运行这个,保存这个代码,例如Transpose.jsee
,然后select这个文件从Select... 在 宏 菜单中。最后,select 运行 Transpose.jsee 在 Macros 菜单中。