Node.jsを使ってC++経由でExcelファイルに画像やシェイプを挿入する
心配しないでください!Aspose.Cellsはこれらの操作をすべてサポートしています。
Excelのシェイプは主に次のタイプに分類されます:
- 写真
- OLEオブジェクト
- 直線
- 四角形
- 基本図形
- ブロック矢印
- 数式図形
- フローチャート
- 星とバナー
- 吹き出し
このガイドドキュメントでは、各タイプから1つまたは2つのシェイプを選び、サンプルを作成します。これらの例を通じて、Aspose.Cellsを使用して指定されたシェイプをワークシートに挿入する方法を学びます。
Node.jsを使用したExcelワークシートへの画像追加
スプレッドシートに写真を追加するのは非常に簡単です。わずかなコード行だけで済みます:
WorksheetオブジェクトのコレクションのPicturesメソッドのPictureCollection.add(number, number, number, number, Uint8Array)を呼び出すだけです。PictureCollection.add(number, number, number, number, Uint8Array)メソッドは以下のパラメータを取ります:
- 左上の行インデックス、左上の行のインデックス。
- 左上の列インデックス、左上の列のインデックス。
- 画像ファイル名、パスを含む画像ファイルの名前。
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"));
Node.jsを使ったExcelワークシートへのOLEオブジェクト挿入
Aspose.Cellsは、ワークシートへのOLEオブジェクトの追加、抽出、および操作をサポートします。そのため、コレクションリストに新しいOLEオブジェクトを追加するために使用されるOleObjectCollectionクラスと、OLEオブジェクトを表すOleObjectクラスがあります。重要なメンバーは次の通りです:
[**OleObject.getImageData()**](https://reference.aspose.com/cells/nodejs-cpp/oleobject/#getImageData--)
プロパティは、イメージ(アイコン)のデータをバイト配列型で指定します。この画像は、ワークシート内のOLEオブジェクトを表示するために使われます。[**OleObject.getObjectData()**](https://reference.aspose.com/cells/nodejs-cpp/oleobject/#getObjectData--)
プロパティは、オブジェクトのデータをバイト配列の形式で指定します。このデータは、ダブルクリックによって関連付けられたプログラムで表示されます。
以下の例は、ワークシートにOLEオブジェクトを追加する方法を示しています。
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");
// Instantiate a new Workbook.
const workbook = new AsposeCells.Workbook();
// Get the first worksheet.
const sheet = workbook.getWorksheets().get(0);
// Define a string variable to store the image path.
const imageUrl = path.join(dataDir, "logo.jpg");
// Get the picture into the streams.
const imageData = fs.readFileSync(imageUrl);
// Get an excel file path in a variable.
const excelFilePath = path.join(dataDir, "book1.xls");
// Get the file into the streams.
const objectData = fs.readFileSync(excelFilePath);
// Add an Ole object into the worksheet with the image
// Shown in MS Excel.
sheet.getOleObjects().add(14, 3, 200, 220, imageData);
// Set embedded ole object data.
sheet.getOleObjects().get(0).setObjectData(objectData);
// Save the excel file
workbook.save(path.join(dataDir, "output.out.xls"));
Node.jsを使用したExcelワークシートへのライン挿入
ラインの形状はlinesカテゴリに属します。
Microsoft Excel(例: 2007年)
- 線を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- それから、『最近使ったシェイプ』または『ライン』からラインを選択します。
Aspose.Cells を使用
ワークシートに線を挿入するために以下の方法を使用できます。
このメソッドは、LineShapeオブジェクトを返します。
以下の例では、ワークシートにラインを挿入する方法を示しています。
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, "sample.xlsx");
// Create workbook from sample file
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the line to the worksheet
sheet.getShapes().addLine(2, 0, 2, 0, 100, 300); // method 1
// sheet.getShapes().addAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300); // method 2
// sheet.getShapes().addShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300); // method 3
// Save. You can check your line in this way.
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使ったExcelワークシートへの矢印ライン挿入
矢印ラインの形状はLinesカテゴリに属しており、ラインの特殊なケースです。
Microsoft Excel(例: 2007年)
- 矢印を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- それから、『最近使ったシェイプ』または『ライン』から矢印ラインを選択します。
Aspose.Cells を使用
ワークシートに直線矢印を挿入するために以下の方法を使用できます。
このメソッドは、LineShapeオブジェクトを返します。
以下の例では、ワークシートに矢印ラインを挿入する方法を示しています。
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, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the line arrow to the worksheet
let s = sheet.getShapes().addLine(2, 0, 2, 0, 100, 300); // method 1
// let s = sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.Line, 2, 0, 2, 0, 100, 300); // method 2
// let s = sheet.getShapes().addShape(AsposeCells.MsoDrawingType.Line, 2, 0, 2, 0, 100, 300); // method 3
// add a arrow at the line begin
s.getLine().setBeginArrowheadStyle(AsposeCells.MsoArrowheadStyle.Arrow); // arrow type
s.getLine().setBeginArrowheadWidth(AsposeCells.MsoArrowheadWidth.Wide); // arrow width
s.getLine().setBeginArrowheadLength(AsposeCells.MsoArrowheadLength.Short); // arrow length
// add a arrow at the line end
s.getLine().setEndArrowheadStyle(AsposeCells.MsoArrowheadStyle.ArrowOpen); // arrow type
s.getLine().setEndArrowheadWidth(AsposeCells.MsoArrowheadWidth.Narrow); // arrow width
s.getLine().setEndArrowheadLength(AsposeCells.MsoArrowheadLength.Long); // arrow length
// Save. You can check your arrow in this way.
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使ったExcelワークシートへの四角形挿入
四角形の形状はRectanglesカテゴリに属します。
Microsoft Excel(例: 2007年)
- 長方形を挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- それから、『最近使ったシェイプ』または『四角形』から選択します。
Aspose.Cells を使用
ワークシートに長方形を挿入するには、次のメソッドを使用できます。
このメソッドは、RectangleShapeオブジェクトを返します。
以下の例では、ワークシートに四角形を挿入する方法を示しています。
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, "sample.xlsx");
// Create workbook from sample file
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the rectangle to the worksheet
sheet.getShapes().addRectangle(2, 0, 2, 0, 100, 300);
// Save
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使ってExcelシートにキューブを挿入する
キューブの形状は基本図形カテゴリに属します。
Microsoft Excel(例: 2007年)
- キューブを挿入したいセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- その後、基本図形からキューブを選択します
Aspose.Cells を使用
ワークシートにキューブを挿入するには、次のメソッドを使用できます。
このメソッドはShapeオブジェクトを返します。
例は、ワークシートにキューブを挿入する方法を示しています。
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, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the cube to the worksheet
sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.Cube, 2, 0, 2, 0, 100, 300);
// Save. You can check your cube in this way.
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使ったExcelシートへのコールアウトクアッドアローの挿入
コールアウトクアッドアローの形状はブロック矢印カテゴリに属します。
Microsoft Excel(例: 2007年)
- コールアウト四角矢印を挿入したいセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- その後、ブロック矢印からコールアウトクアッドアローを選びます
Aspose.Cells を使用
ワークシートにコールアウト四角矢印を挿入するには、次のメソッドを使用できます。
このメソッドはShapeオブジェクトを返します。
例は、ワークシートにコールアウトクアッドアローを挿入する方法を示しています。
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, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the callout quad arrow to the worksheet
sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.QuadArrowCallout, 2, 0, 2, 0, 100, 100);
//Save
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使ったExcelシートへの掛け算記号の挿入
掛け算記号の形状は式図形カテゴリに属します。
Microsoft Excel(例: 2007年)
- 乗算記号を挿入したいセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- その後、式図形から掛け算記号を選択します
Aspose.Cells を使用
次の方法を使用してワークシートに乗算記号を挿入できます。
このメソッドはShapeオブジェクトを返します。
例は、ワークシートに掛け算記号を挿入する方法を示しています。
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, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the multiplication sign to the worksheet
sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.MathMultiply, 2, 0, 2, 0, 100, 100);
// Save. You can check your multiplication in this way.
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使ったExcelシートへの多ドキュメントの挿入
多ドキュメントの形状はフローチャートカテゴリに属します。
Microsoft Excel(例: 2007年)
- 多重ドキュメントを挿入するセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- その後、多ドキュメントをフローチャートから選択します
Aspose.Cells を使用
次の方法を使用してワークシートに多重ドキュメントを挿入できます。
このメソッドはShapeオブジェクトを返します。
例は、多ドキュメントをワークシートに挿入する方法を示しています。
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, "sample.xlsx");
// Create workbook from sample file
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the multidocument to the worksheet
sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.FlowChartMultidocument, 2, 0, 2, 0, 100, 100);
// Save
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使用してExcelワークシートに五芒星を挿入する
五芒星の形状は星とバナーカテゴリに属しています。
Microsoft Excel(例: 2007年)
- 五角星を挿入したいセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- その後、星とバナーから五芒星を選択します
Aspose.Cells を使用
この方法は Shape オブジェクトを返します。
このメソッドはShapeオブジェクトを返します。
次の例は、ワークシートに五芒星を挿入する方法を示しています。
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, "sample.xlsx");
// Create workbook from sample file
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the Five-pointed star to the worksheet
sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.Star5, 2, 0, 2, 0, 100, 100);
// Save. You can check your icon in this way.
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。
Node.jsを使用してExcelワークシートに思考バブル雲を挿入する
思考バブル雲の形状はコールアウトカテゴリに属しています。
Microsoft Excel(例: 2007年)
- 思考バブルクラウドを挿入したいセルを選択します
- [挿入] メニューをクリックし、[図形] をクリックします。
- その後、コールアウトから思考バブル雲を選択します
Aspose.Cells を使用
ワークシートに思考バブルクラウドを挿入するために次の方法を使用できます。
このメソッドはShapeオブジェクトを返します。
次の例は、ワークシートに思考バブル雲を挿入する方法を示しています。
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, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the thought bubble cloud to the worksheet
sheet.getShapes().addAutoShape(AsposeCells.AutoShapeType.CloudCallout, 2, 0, 2, 0, 100, 100);
// Save
workbook.save("sample.xlsx", AsposeCells.SaveFormat.Xlsx);
上記のコードを実行すると、次の結果が得られます。