Node.jsとC++で行列や列の書式設定

行で操作する

行の高さの調整方法

Aspose.Cellsは、WorkbookというMicrosoft Excelファイルを表すクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionがあります。ワークシートはWorksheetクラスで表され、Worksheetクラスはすべてのセルを表すCellsコレクションを提供します。

Cellsコレクションは、ワークシート内の行や列を管理するための複数のメソッドを提供します。以下に詳細を説明します。

行の高さを設定する方法

単一の行の高さを設定するには、CellsコレクションのsetRowHeight(number, number)メソッドを呼び出します。setRowHeight(number, number)メソッドは次のパラメータを取ります:

  • 行インデックス:高さを変更する行のインデックス。
  • 行の高さ:行に適用する行の高さ。
try {
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.createReadStream(filePath);

// Reading the file stream into a buffer
const chunks = [];
fstream.on('data', chunk => chunks.push(chunk));
fstream.on('end', () => {
const buffer = Buffer.concat(chunks);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(buffer);

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

// Setting the height of the second row to 13
worksheet.getCells().setRowHeight(1, 13);

// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));

// Closing the file stream to free all resources
fstream.close();

ワークシート内のすべての行の高さを設定する方法

全ての行の同じ高さを設定したい場合は、CellsコレクションのgetStandardHeight()プロパティを使用して行えます。

例:

const fs = require("fs");
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");

// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

// Setting the height of all rows in the worksheet to 15
worksheet.getCells().setStandardHeight(15);

// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));

// Closing the file stream to free all resources
// (Note: Closing the file stream is unnecessary in this context as it's a 
// synchronous read performed using fs.readFileSync, which does not require
// explicit closure, but if using fs.createReadStream, you would handle it accordingly)

列で操作する

列の幅を設定する方法

列の幅を設定するには、CellsコレクションのsetColumnWidth(number, number)メソッドを呼び出します。setColumnWidth(number, number)メソッドは次のパラメータを取ります:

  • 列インデックス:幅を変更する列のインデックス。
  • 列の幅:設定したい列の幅。
const fs = require("fs");
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");

// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);

// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

// Setting the width of the second column to 17.5
worksheet.getCells().setColumnWidth(1, 17.5);

// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));

// Closing the file stream to free all resources
fstream; // Note: No explicit close needed for fs.readFileSync

ピクセル単位で列幅を設定する方法

列の幅を設定するには、CellsコレクションのsetColumnWidthPixel(number, number)メソッドを呼び出します。setColumnWidthPixel(number, number)メソッドは次のパラメータを取ります:

  • 列インデックス:幅を変更する列のインデックス。
  • 列の幅、ピクセル単位での所望の列の幅。
const path = require("path");
const AsposeCells = require("aspose.cells.node");

// Source directory
const sourceDir = path.join(__dirname, "data");
const outDir = path.join(__dirname, "output");

// Load source Excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Book1.xlsx"));

// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);

// Set the width of the column in pixels
worksheet.getCells().setColumnWidthPixel(7, 200);

workbook.save(path.join(outDir, "SetColumnWidthInPixels_Out.xlsx"));

ワークシート内のすべての列の幅を設定する方法

全列に対して同じ幅を設定したい場合は、CellsコレクションのgetStandardWidth()プロパティを使用します。

const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "book1.xls");

// Instantiating a Workbook object
// Opening the Excel file
const workbook = new AsposeCells.Workbook(filePath);

// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);

// Setting the width of all columns in the worksheet to 20.5
worksheet.getCells().setStandardWidth(20.5);

// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));

// Closing the file stream to free all resources
// No explicit close needed in Node.js

高度なトピック