EmEditorみんなでまとめサイト

箱形選択領域に連番を挿入するマクロ

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集

説明

  • 箱形選択された領域に連番を挿入する
  • 箱形選択して実行すると開始番号の入力を求められる
  • そのときに、数字の前方に空白や0をいれると、先頭文字を使い文字列長にあわせて桁をあわせる

既知の問題

  • 実行後、未選択状態になる
  • 実行後、カーソル位置が元の選択領域の左上の位置にしてしまっている
  • 「元に戻す」で一気に戻せない
  • 折り返しを気にして、後ろから処理してしまっている
  • 開始番号を負数にすると桁数あわせ処理が原因でおかしなことになる

ソース(.jsee)

if( document.selection.IsEmpty || (document.selection.Mode & eeModeMask) != eeModeBox ){
   alert('箱形選択してください');
}else{
   //  開始番号の入力を求める
   var ScriptControl = new ActiveXObject('ScriptControl');
   ScriptControl.Language = 'VBScript';
   ScriptControl.AddCode('Function Input(x,y,z)\nInput=InputBox(x,y,z)\nEnd Function');
   var str = ScriptControl.Run('Input', '開始番号を入力してください', '連番挿入マクロ', '1');
   if( !str ) Quit();
   var no = Number( str );
   while( isNaN(no) ){
       str = ScriptControl.Run('Input', '開始"番号"を入力してください', '連番挿入マクロ', str);
       if( !str ) Quit();
       no = Number( str );
   }

   //  桁あわせのための情報を得る
   var digit = str.length;
   var fill  = str.substring(0,1);
   if( fill.length <= 0 ) fill = ' ';

   //  選択領域の情報を得る
   var tx = document.selection.GetTopPointX(    eePosView );
   var ty = document.selection.GetTopPointY(    eePosView );
   var bx = document.selection.GetBottomPointX( eePosView );
   var by = document.selection.GetBottomPointY( eePosView );
   var lx = (tx < bx) ? tx : bx, rx = (tx > bx) ? tx : bx, y, n = by - ty + no;

   //  挿入
   for( y = by ; y >= ty ; --y, --n ){
       var s = '' + n;
       while( s.length < digit ) s = fill + s;
       document.selection.SetActivePoint( eePosView, lx, y, false );
       document.selection.SetActivePoint( eePosView, rx, y, true );
       document.selection.Text = s;
   }

   //  カーソルを、とりあえず元の選択領域の左上の位置にする
   document.selection.SetActivePoint( eePosView, lx, ty, false );
}

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

目安箱バナー