Node.jsとC++を使った写真の管理
Aspose.Cellsを使用すると、開発者は実行時にスプレッドシートに画像を追加できます。さらに、これらの画像の位置を実行時に制御することができます。これについては後のセクションで詳しく説明します。
この資料では、画像の追加方法や特定のセルの内容を示す画像の挿入方法について解説します。
画像の追加
スプレッドシートに写真を追加するのは非常に簡単です。わずかなコード行だけで済みます:
WorksheetオブジェクトのコレクションのPicturesメソッドのAddを呼び出すだけです。Addメソッドは以下のパラメータを取ります:
- 左上の行インデックス、左上の行のインデックス。
- 左上の列インデックス、左上の列のインデックス。
- 画像ファイル名、パスを含む画像ファイルの名前。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
worksheet.getPictures().add(5, 5, path.join(dataDir, "logo.jpg"));
// Saving the Excel file
workbook.save(path.join(dataDir, "output.xls"));
写真の位置合わせ
Aspose.Cellsを使用して写真の位置合わせを制御する方法には2つの方法があります:
- 比例位置合わせ:行の高さと幅に比例した位置を定義します。
- 絶対位置指定:画像を挿入するページ上の正確な位置を定義します。例:セルの縁から左に40ピクセル、下に20ピクセル。
比例位置合わせ
開発者は、PictureオブジェクトのgetUpperDeltaX()とgetUpperDeltaY()のプロパティを使用して、行の高さと列の幅に比例した画像の位置を設定できます。Picturesコレクションから画像のインデックスを渡すことで、Pictureオブジェクトを取得できます。この例では、F6セルに画像を配置しています。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
const pictureIndex = worksheet.getPictures().add(5, 5, path.join(dataDir, "logo.jpg"));
// Accessing the newly added picture
const picture = worksheet.getPictures().get(pictureIndex);
// Positioning the picture proportional to row height and column width
picture.setUpperDeltaX(200);
picture.setUpperDeltaY(200);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
絶対位置づけ
開発者は、PictureオブジェクトのgetLeft()とgetTop()のプロパティを使用して、画像を絶対位置に配置することも可能です。この例では、セルF6に画像を配置し、左から60ピクセル、上から10ピクセルの位置にあります。
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "logo.jpg");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Workbook object
const sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
const pictureIndex = worksheet.getPictures().add(5, 5, filePath);
// Accessing the newly added picture
const picture = worksheet.getPictures().get(pictureIndex);
// Absolute positioning of the picture in unit of pixels
picture.setLeft(60);
picture.setTop(10);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
セル参照に基づいて画像を挿入
Aspose.Cellsを使用すると、ワークシートのセルの内容を画像形状で表示できます。画像は、データを表示したいセルにリンクされています。セルまたはセル範囲がグラフィックオブジェクトにリンクされているため、そのセルまたはセル範囲のデータを変更すると、自動的にグラフィックオブジェクトに変更が反映されます。
ShapeCollectionコレクションのWorksheetオブジェクトのShapeCollection.addPicture(number, number, number, number, Uint8Array)メソッドを呼び出して、ワークシートに画像を追加します。セル範囲は、PictureオブジェクトのgetFormula()属性を使用して指定します。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a new Workbook
const workbook = new AsposeCells.Workbook();
// Get the first worksheet's cells collection
const cells = workbook.getWorksheets().get(0).getCells();
// Add string values to the cells
cells.get("A1").putValue("A1");
cells.get("C10").putValue("C10");
const picts = workbook.getWorksheets().get(0).getPictures();
// Add a blank picture to the D1 cell
const picIndex = picts.add(0, 3, 10, 6, null);
const pic = picts.get(picIndex);
// Specify the formula that refers to the source range of cells
pic.setFormula("A1:C10");
// Update the shapes selected value in the worksheet
workbook.getWorksheets().get(0).getShapes().updateSelectedValue();
// Save the Excel file.
workbook.save(path.join(dataDir, "output.out.xls"));