行・列・セル範囲の操作 — 挿入・削除・クリア・コピー
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 を使います。
| 値 | 数値 | 用途 |
|---|---|---|
InsertDirectionRight | 1 | セル挿入時に右へずらす |
InsertDirectionDown | 2 | セル挿入時に下へずらす |
DeleteDirectionLeft | 3 | セル削除時に左へずらす |
DeleteDirectionUp | 4 | セル削除時に上へずらす |
コード 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)。
解説
- 行番号・列番号はすべて 1 始まりの数値。 列は
"A"ではなく1で渡します。
文字列から変換する場合はapp.convertToColumnNumber("B")→2を使います。 insert/deleteはデータをずらし、clearはずらしません。 行を空にしたいだけならclearRow()(既存データの位置は保たれる)、 行そのものを無くしたいならdeleteRow()を使います。copy系はコピー元を「シート名」で指定します。 自シート内コピーもシート名を渡します。
第5引数copyTypeにenums.XlCopyContentTypeを渡すとコピー対象を絞れます。
| 値 | 数値 | コピー対象 | |---|---|---| |CopyContentTypeAll| 0 | 値、セル書式、関数セルのセル書式(既定) | |CopyContentTypeValueOnly| 1 | 値のみ | |CopyContentTypeValueAndFormat| 2 | 値とセル書式 | |CopyContentTypeFormatOnly| 3 | セル書式のみ |Col.getWidth()とCol.getColumnWidth()は単位が違います。 実測で、既定状態の A 列はgetWidth()= 72、getColumnWidth()= 8.38 でした。
setColumnWidth(20)の後はgetWidth()= 165、getColumnWidth()= 20 です。
設定できるのはsetColumnWidth()(文字幅)だけで、getWidth()は読み取り専用です。setBestFit(true)は設定時のセル内容に合わせて列幅を再計算します。 後に内容を書き換えても自動では追従しないので、データを入れ終えてから呼んでください。- アウトライン(グループ化)は
WorkSheetのメソッド。setRowOutline(startRow, endRow, level)/setColOutline(startCol, endCol, level)で設定し、clearRowOutline(level)/clearColOutline(level)で解除します。
結果はRow.getOutlineLevel()/Col.getOutlineLevel()で確認できます。
注意点
insertCell/deleteCellのdirectionは操作に合わせて使い分けます。 挿入はInsertDirectionRight(1) /InsertDirectionDown(2)、 削除はDeleteDirectionLeft(3) /DeleteDirectionUp(4) が本来の指定です。copy系でコピーされないものがあります。 コピー対象をCopyContentTypeAllにしても、 図形(グラフを含む)・ピボットテーブル・queryTable 形式のテーブル・データテーブルは コピーされません。copyCellの第3引数は「コピー先の左上セル」1 つだけ。copyCell("Sheet1", "A3:B4", "F8")は F8:G9 に展開されます(範囲の大きさはコピー元から決まる)。copyRow/copyColのコピー元はシート名、位置は行番号・列番号(数値)。 セル範囲の文字列(A1 形式)は使いません。
混同しやすいので注意してください。
関連
- 目次
- 前: セル値の型と表示書式 / 次: 大量データの一括投入
- API リファレンス: WorkSheet / Row / Col / XlInsertDirection / XlCopyContentType