2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

常用sakuraマクロ

Posted at

行頭の空白削除

S_ReplaceAll('^ +', '', 44);	// すべて置換
S_ReDraw(0);	// 再描画

スネークからキャメルに変換

S_ReplaceAll('_a', 'A', 40);
S_ReplaceAll('_b', 'B', 40);
S_ReplaceAll('_c', 'C', 40);
S_ReplaceAll('_d', 'D', 40);
S_ReplaceAll('_e', 'E', 40);
S_ReplaceAll('_f', 'F', 40);
S_ReplaceAll('_g', 'G', 40);
S_ReplaceAll('_h', 'H', 40);
S_ReplaceAll('_i', 'I', 40);
S_ReplaceAll('_j', 'J', 40);
S_ReplaceAll('_k', 'K', 40);
S_ReplaceAll('_l', 'L', 40);
S_ReplaceAll('_m', 'M', 40);
S_ReplaceAll('_n', 'N', 40);
S_ReplaceAll('_o', 'O', 40);
S_ReplaceAll('_p', 'P', 40);
S_ReplaceAll('_q', 'Q', 40);
S_ReplaceAll('_r', 'R', 40);
S_ReplaceAll('_s', 'S', 40);
S_ReplaceAll('_t', 'T', 40);
S_ReplaceAll('_u', 'U', 40);
S_ReplaceAll('_v', 'V', 40);
S_ReplaceAll('_w', 'W', 40);
S_ReplaceAll('_x', 'X', 40);
S_ReplaceAll('_y', 'Y', 40);
S_ReplaceAll('_z', 'Z', 40);

S_ReDraw(0);	// 再描画

改行消す

S_ReplaceAll('\\r\\n', '', 44);	// すべて置換
S_ReplaceAll('\\n', '', 44);	// すべて置換
S_ReplaceAll('\\r', '', 44);	// すべて置換
S_ReplaceAll(' ', '', 44);	// すべて置換
S_ReplaceAll('	', '', 44);	// すべて置換
S_ReDraw(0);	// 再描画

保存する

FileSave();

閉じる

WinClose();

Json整形

// 行数を取得
var cnt = GetLineCount(0);
// 改行を除いた行文字列を取得
var textAll = "";
for (var i = 1; i <= cnt; i++) {
    var str = GetLineStr(i).replace(/\r\n/,"").replace(/\n/,"");
    textAll += str;
}
var editJson = "";
var innerFlg = false;
var intentLv = 0;
var quoteType = [];
var blockType = [];
var preChr = "";
for (var i = 0; i < textAll.length; i++) {
    var prefix = "";
    var suffix = "";
    var c = textAll.substring(i, i + 1);
    if (!innerFlg) {
        if (c == " ") {
            continue;
        }
        if (c == "\"" || c == "'"){
            innerFlg = true;
            quoteType[quoteType.length] = c;
            // JSON または 配列の1つ目の要素の時はスペース1個を追加
            var chkText = editJson.replace(/( |\t|\r\n|\n)/g, "");
            if (chkText.length > 0) {
                var preChar = chkText.substring(chkText.length - 1, chkText.length);
                if (preChar == "[" || preChar == "{") {
                    c = " " + c;
                }
            }
        } else {
            if (c == "{" || c == "[") {
                suffix = "\n";
                intentLv = intentLv + 1;
                blockType[blockType.length] = c;
                for (var j = 0; j < intentLv; j++) {
                    suffix = suffix + "\t";
                }
            }
            if (c == "}" || c == "]") {
                prefix = "\n";
                suffix = "\n";
                intentLv = intentLv - 1;
                blockType.pop(1);
            }
            if (c == ",") {
                prefix = "\n";
            }
        }
    } else {
        if (quoteType.length > 0 && c == quoteType[quoteType.length - 1]){
            innerFlg = false;
        }
    }
    if (prefix != "") {
        for (var j = 0; j < intentLv; j++) {
            prefix = prefix + "\t";
        }
    }
    var line = "" + prefix + c + suffix;
    if (preChr == "\n" && line.substring(0, 1) == "\n") {
        line = line.substring(1);
    }
    editJson = editJson + "" + line;
    preChr = line.substring(line.length - 1, line.length);
}
SelectAll(0);
InsText(editJson + "\n");

ゼロ埋めする

(function(){
    String.prototype.repeat = function(count){
        var result = '';
        for(var i = 0; i < count; i++){
            result += this;
        }
        return result;
    };
    String.prototype.padLeft = function(length, char){
        return char.repeat(length - this.length) + this;
    };
    
    if(Editor.IsTextSelected() == 0) Editor.SelectAll();
    
    var selection = Editor.GetSelectedString(0);
    var matches = selection.match(/\d+/g);
    var maxlength = 0;
    for(var i = 0; i < matches.length; i++){
        if(maxlength < matches[i].length) maxlength = matches[i].length;
    }
    
    var result = selection.replace(/\d+/g, function(m){
        return m.padLeft(maxlength, '0');
    });
    
    Editor.InsText(result);
})();

連番生成するやつ

(function(){
    String.prototype.chomp = function(){
        return this.replace(/[\r\n]+$/, '');
    };
    
    function get_upline(){
        Editor.GoLineTop();
        Editor.Up_Sel();
        return Editor.GetSelectedString(0).chomp();
    }
    
    if(Editor.IsTextSelected() != 0) return;
    
    var end_value = get_upline();
    var begin_value = get_upline();
    if(!/^\d+$/.test(end_value) || !/^\d+$/.test(begin_value)) return;
    
    var result = [];
    for(var i = parseInt(begin_value); i <= parseInt(end_value); i++){
        result.push(i.toString());
    }
    
    var n = ['\r\n', '\r', '\n'][Editor.GetLineCode()];
    Editor.GoLineTop();
    Editor.Down_Sel();
    Editor.Down_Sel();
    Editor.InsText(result.join(n) + n);
})();

改行をけす

置換前:^\r\n
置換後:(空白)
正規表現にチェック
範囲:選択範囲

行頭に挿入する

1. Ctrl + R
2. 正規表現にチェックを入れる
3. 置換前に「^」を入力
4. 置換後に挿入したい文字を入力

行末に挿入する

1. Ctrl + R
2. 正規表現にチェックを入れる
3. 置換前に「$」を入力
4. 置換後に挿入したい文字を入力
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?