行・列・セル範囲の操作 — 挿入・削除・クリア・コピー

level: basic / verified: 2026-10-01 / 4言語タブ対応 / English

やりたいこと

ワークシート上の行・列・セル範囲を、挿入 / 削除 / クリア / コピーする。 あわせて行の高さ・列の幅・非表示などを Row / Col オブジェクトで設定する。

4 つの操作の違い

操作挙動行・列の場合セル範囲の場合
insert空白を挿入し、既存データをずらすinsertRow(start, n) / insertCol(start, n)insertCell(A1C1, direction)
delete削除して、既存データをずらすdeleteRow(start, n) / deleteCol(start, n)deleteCell(A1C1, direction)
clear中身と書式を初期状態に戻す。ずらさないclearRow(start, n) / clearCol(start, n)clearCell(A1C1)
copy別シート(または自シート)から上書きコピーcopyRow(fromSheet, fromRow, n, toRow, copyType?)copyCell(fromSheet, fromA1C1, toA1, copyType?)

insertCell / deleteCell の direction は enums.XlInsertDirection を使います。

値数値用途
InsertDirectionRight1セル挿入時に右へずらす
InsertDirectionDown2セル挿入時に下へずらす
DeleteDirectionLeft3セル削除時に左へずらす
DeleteDirectionUp4セル削除時に上へずらす

コード 1: 行・列・セル範囲の操作

実行結果:

// rows-cols.js — 行・列・セル範囲の挿入/削除/クリア/コピー
const { nodeosbxl, enums } = require("nodeosbxl");

const app = new nodeosbxl.App();
const wb = app.createWorkBook("rows-cols.xlsx");
const ws = wb.openWorkSheet("Sheet1");

// 行 → 列 の順に並べて 1 行で表示する(· は空セル)
const dump = (label, range) => {
    const v = ws.getRange(range).getValue();
    const keys = Object.keys(v).map((k) => /^([A-Z]+)(\d+)$/.exec(k)).sort((a, b) =>
        Number(a[2]) - Number(b[2]) ||
        app.convertToColumnNumber(a[1]) - app.convertToColumnNumber(b[1]));
    console.log(`${label} ${keys.map((m) => `${m[1]}${m[2]}=${v[m[1] + m[2]] || "·"}`).join(" ")}`);
};
// A1:C5 を r{行}c{列} で埋める
const fill = () => {
    for (let r = 1; r <= 5; r++) for (let c = 1; c <= 3; c++) ws.getCells(r, c).setValue(`r${r}c${c}`);
};

fill();
dump("[初期]       A列:", "A1:A5");

// ---------- 行 ----------
ws.insertRow(3, 2);                    // 3 行目の「前」に 2 行挿入 → 既存データは下へずれる
dump("[insertRow]  A列:", "A1:A7");
ws.deleteRow(3, 2);                    // 3 行目から 2 行削除 → 下の行が上がる
dump("[deleteRow]  A列:", "A1:A5");
ws.clearRow(2, 1);                     // 2 行目をクリア(位置はずらさない)
dump("[clearRow]   A列:", "A1:A5");
ws.copyRow("Sheet1", 1, 1, 4);         // 自シートの 1 行目を 4 行目へ上書きコピー
dump("[copyRow]    A列:", "A1:A5");

fill();
// ---------- 列 ----------
ws.insertCol(2, 1);                    // B 列の前に 1 列挿入
dump("[insertCol] 1行目:", "A1:D1");
ws.deleteCol(2, 1);                    // 挿入した B 列を削除
dump("[deleteCol] 1行目:", "A1:C1");
ws.clearCol(3, 1);                     // C 列をクリア
dump("[clearCol]  1行目:", "A1:C1");
fill();
ws.copyCol("Sheet1", 1, 2, 5);         // A,B 列を E,F 列へコピー
dump("[copyCol]   1行目:", "A1:F1");

// ---------- セル範囲 ----------
ws.insertCell("B2:B3", enums.XlInsertDirection.InsertDirectionDown);
dump("[insertCell] B列:", "B1:B5");
ws.deleteCell("B2:B3", enums.XlInsertDirection.DeleteDirectionUp);
dump("[deleteCell] B列:", "B1:B5");
ws.clearCell("A1:B2");
dump("[clearCell] A1:B2:", "A1:B2");
ws.copyCell("Sheet1", "A3:B4", "F8");
dump("[copyCell]  F8:G9:", "F8:G9");

wb.save();
wb.close();
[初期]       A列: A1=r1c1 A2=r2c1 A3=r3c1 A4=r4c1 A5=r5c1
[insertRow]  A列: A1=r1c1 A2=r2c1 A3=· A4=· A5=r3c1 A6=r4c1 A7=r5c1
[deleteRow]  A列: A1=r1c1 A2=r2c1 A3=r3c1 A4=r4c1 A5=r5c1
[clearRow]   A列: A1=r1c1 A2=· A3=r3c1 A4=r4c1 A5=r5c1
[copyRow]    A列: A1=r1c1 A2=· A3=r3c1 A4=r1c1 A5=r5c1
[insertCol] 1行目: A1=r1c1 B1=· C1=r1c2 D1=r1c3
[deleteCol] 1行目: A1=r1c1 B1=r1c2 C1=r1c3
[clearCol]  1行目: A1=r1c1 B1=r1c2 C1=·
[copyCol]   1行目: A1=r1c1 B1=r1c2 C1=r1c3 D1=· E1=r1c1 F1=r1c2
[insertCell] B列: B1=r1c2 B2=· B3=· B4=r2c2 B5=r3c2
[deleteCell] B列: B1=r1c2 B2=r2c2 B3=r3c2 B4=r4c2 B5=r5c2
[clearCell] A1:B2: A1=· B1=· A2=· B2=·
[copyCell]  F8:G9: F8=r3c1 G8=r3c2 F9=r4c1 G9=r4c2

· は空セルです。insertRow で 3・4 行目が空白になり既存データが下へずれたこと、 clearRow では位置がずれていないこと、copyRow で 1 行目の内容が 4 行目に上書きされたことが読み取れます。

コード 2: Row / Col オブジェクト

実行結果:

// row-col-props.js — 行の高さ・列の幅・非表示・自動調整
const { nodeosbxl } = require("nodeosbxl");

const app = new nodeosbxl.App();
const wb = app.createWorkBook("row-col-props.xlsx");
const ws = wb.openWorkSheet("Sheet1");

// ---------- Row ----------
const row = ws.getRow(1);                        // 1 行目
console.log("既定の高さ      =", row.getHeight());
row.setHeight(30);
console.log("setHeight(30)後 =", row.getHeight());
console.log("getHidden 既定  =", row.getHidden(), "/ getOutlineLevel =", row.getOutlineLevel());
ws.getRow(2).setHidden(true);                    // 2 行目を非表示
console.log("2行目 hidden    =", ws.getRow(2).getHidden());

// ---------- Col ----------
const col = ws.getCol(1);                        // 1 列目(A 列)。列番号は数値で渡す
console.log("既定の列幅      = columnWidth:", col.getColumnWidth(), "/ width:", col.getWidth());
col.setColumnWidth(20);
console.log("setColumnWidth(20) -> columnWidth=" + col.getColumnWidth(),
            "/ width=" + col.getWidth());
ws.getCol(2).setHidden(true);                    // B 列を非表示
console.log("B列 hidden      =", ws.getCol(2).getHidden());

ws.getRange("C1:C1").setValue("自動調整のサンプル文字列");
console.log("C列 bestFit 既定 =", ws.getCol(3).getBestFit());
ws.getCol(3).setBestFit(true);                   // 内容に合わせて列幅を自動調整
console.log("setBestFit(true) -> bestFit =", ws.getCol(3).getBestFit(),
            "/ columnWidth =", ws.getCol(3).getColumnWidth());

// アウトライン(グループ化)は WorkSheet 側で設定する
ws.setRowOutline(4, 6, 1);                       // 4〜6 行目をレベル 1 でグループ化
ws.setColOutline(2, 3, 1);                       // B〜C 列をレベル 1 でグループ化
console.log("4行目 outlineLevel =", ws.getRow(4).getOutlineLevel(),
            "/ B列 outlineLevel =", ws.getCol(2).getOutlineLevel());

wb.save();
wb.close();
既定の高さ      = 18.75
setHeight(30)後 = 30
getHidden 既定  = false / getOutlineLevel = 0
2行目 hidden    = true
既定の列幅      = columnWidth: 8.38 / width: 72
setColumnWidth(20) -> columnWidth=20 / width=165
B列 hidden      = true
C列 bestFit 既定 = false
setBestFit(true) -> bestFit = true / columnWidth = 25.38
4行目 outlineLevel = 1 / B列 outlineLevel = 1

setBestFit(true) による列幅は、セルの内容とブックの既定フォントから算出されるため 環境によって値が変わります(上記は 25.38)。

解説

注意点

関連