Node.jsとC++を使用して行と列を自動調整

自動調整

Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイルの各シートにアクセスできるWorkbook.getWorksheets()コレクションが含まれています。シートはWorksheetクラスによって表されます。Worksheetクラスはシート管理のための多くのプロパティとメソッドを提供します。この記事では、Worksheetクラスを使用した行や列の自動調整について説明します。

行の自動調整 - シンプル

行の幅と高さを自動調整する最も簡単な方法は、WorksheetクラスのautoFitRowメソッドを呼び出すことです。autoFitRowメソッドは調整対象の行のインデックスをパラメータとして受け取ります。

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

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

// Reading the Excel file into a buffer
const fs = require("fs");
const fileBuffer = fs.readFileSync(inputPath);

// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileBuffer);

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

// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1);

// Saving the modified Excel file
const outputPath = path.join(dataDir, "output.xlsx");
workbook.save(outputPath);

セル範囲内の行を自動調整する方法

行は多くの列で構成されます。Aspose.Cellsでは、行内のセル範囲の内容に基づいて行を自動調整できるように、autoFitRowメソッドのオーバーロード版を呼び出すことが可能です。引数は次の通りです。

  • 行インデックス:自動調整される行のインデックス。
  • 最初の列インデックス:行の最初の列のインデックス。
  • 最後の列インデックス:行の最後の列のインデックス。

autoFitRowメソッドは、行内のすべての列の内容をチェックし、その後行を自動調整します。

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

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

// Reading the Excel file into a buffer
const fs = require("fs");
const fileData = fs.readFileSync(inputPath);

// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileData);

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

// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1, 0, 5);

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

セル範囲内の列を自動調整する方法

列は多くの行から構成されます。列内のセル範囲の内容に基づいて列を自動調整するには、次のパラメータを受け取るautoFitColumnメソッドのオーバーロード版を呼び出します。

  • 列インデックス:自動調整される列のインデックス。
  • 最初の行インデックス:列の最初の行のインデックス。
  • 最後の行インデックス:列の最後の行のインデックス。

autoFitColumnメソッドは、列内のすべての行の内容をチェックし、その後列を自動調整します。

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

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

// Creating a file stream containing the Excel file to be opened
const fs = require("fs");
const workbook = new AsposeCells.Workbook(fs.readFileSync(inputPath));

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

// Auto-fitting the Column of the worksheet
worksheet.autoFitColumn(4);

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

結合セルの行を自動調整する方法

Aspose.Cellsを使用すれば、AutoFitterOptions APIを用いて結合セルを含むセルの行も自動調整可能です。AutoFitterOptionsクラスは、結合セル用に行自動調整に使えるAutoFitterOptions.getAutoFitMergedCellsType()プロパティを提供しています。AutoFitterOptions.getAutoFitMergedCellsType()は、AutoFitMergedCellsType列挙可能な値と次のメンバーを受け入れます。

  • なし: 結合セルを無視します。
  • 最初の行のみ: 最初の行の高さのみ拡張します。
  • 最終行のみ: 最後の行の高さのみ拡張します。
  • 各行: 各行の高さのみ拡張します。
const path = require("path");
const AsposeCells = require("aspose.cells.node");

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

// Instantiate a new Workbook
const wb = new AsposeCells.Workbook();

// Get the first (default) worksheet
const worksheet = wb.getWorksheets().get(0);

// Create a range A1:B1
const range = worksheet.getCells().createRange(0, 0, 1, 2);

// Merge the cells
range.merge();

// Insert value to the merged cell A1
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");

// Create a style object
const style = worksheet.getCells().get(0, 0).getStyle();

// Set wrapping text on
style.setIsTextWrapped(true);

// Apply the style to the cell
worksheet.getCells().get(0, 0).setStyle(style);

// Create an object for AutoFitterOptions
const options = new AsposeCells.AutoFitterOptions();

// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AsposeCells.AutoFitMergedCellsType.EachLine);

// Autofit rows in the sheet (including the merged cells)
worksheet.autoFitRows(options);

// Save the Excel file
wb.save(path.join(outputDir, "AutofitRowsforMergedCells.xlsx"));

重要なこと

高度なトピック