我目前有一個大的制表符分隔資料集,看起來像這樣(n 只是一個數字,有超過 4 個標題),一個重復的初始標題列,然后最多 2 列資料(有時只有一個):
輸入檔案:
Hdr1 A1 B1
Hdr2 A2 B2
Hdr3 A3 B3
Hdrn An Bn
Hdr1 C1
Hdr2 C2
Hdr3 C3
Hdrn Cn
Hdr1 D1 E1
Hdr2 D2 E2
Hdr2 D3 E3
Hdrn Dn En
我需要轉置和轉換資料,使輸出看起來與此類似(因此洗掉了重復的標題并保留了資料):
Hdr1 Hdr2 Hdr3 Hdrn
A1 A2 A3 An
B1 B2 B3 Bn
C1 C2 C3 Cn
D1 D2 D3 Dn
E1 E2 E3 En
任何關于如何使用優化的 EmEditor javascript 宏執行此操作的想法將不勝感激。
uj5u.com熱心網友回復:
這是一個 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然后從宏選單中的選擇...中選擇此檔案。最后,在Macros選單中選擇Run Transpose.jsee 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474480.html
