Node.js経由のC++を使って指定されたワークシートをPDFに保存

デフォルトでは、Aspose.Cellsはブック内のすべての表示されているワークシートをPDFに保存します。PdfSaveOptions.getSheetSet() オプションを使用して、特定のワークシートだけをPDFに保存できます。例:アクティブなワークシートをPDFに保存、すべてのワークシート(表示および非表示)をPDFに保存、複数のカスタムワークシートをPDFに保存など。

アクティブワークシートをPDFに保存する

アクティブシートのみをPDFにエクスポートしたい場合は、PdfSaveOptions.getSheetSet()SheetSet.getActive()を渡すことで実現できます。

シート「Sheet2」は、ソースファイルsheetset-example.xlsxのアクティブシートです。

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, "sheetset-example.xlsx");

// Open the template excel file
const workbook = new AsposeCells.Workbook(filePath);

// Set active sheet to output
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(AsposeCells.SheetSet.getActive());

// Save the pdf file with PdfSaveOptions
workbook.save("output.pdf", pdfSaveOptions);

すべてのワークシートをPDFに保存

SheetSet.getVisible()はブック内の表示されているシートを示し、SheetSet.getAll()は表示・非表示に関わらずすべてのシートを含むものを示します。すべてのシートをPDFにエクスポートしたい場合は、SheetSet.getAll()PdfSaveOptions.getSheetSet() オプションに渡すだけです。

ソースファイル sheetset-example.xlsx には、非表示シート Sheet3 を含むすべての4つのシートが含まれています。

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, "sheetset-example.xlsx");

// Open the template excel file
const workbook = new AsposeCells.Workbook(filePath);

// Set all sheets to output
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(AsposeCells.SheetSet.getAll());

// Save the pdf file with PdfSaveOptions
workbook.save("output.pdf", pdfSaveOptions);

指定されたワークシートをPDFに保存

複数の目的のシートやカスタムシートをPDFにエクスポートしたい場合は、複数のシートインデックスをPdfSaveOptions.getSheetSet()に渡すことで実現できます。

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, "sheetset-example.xlsx");

// Open the template excel file
const workbook = new AsposeCells.Workbook(filePath);

// Set custom multiple sheets(Sheet1, Sheet3) to output
const sheetSet = new AsposeCells.SheetSet([0, 2]);
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(sheetSet);

// Save the pdf file with PdfSaveOptions
workbook.save("output.pdf", pdfSaveOptions);

ワークシートを並べ替えてPDFに変換

ソースファイルを変更せずにシートの順序(例:逆順)を変更してPDFに出力したい場合は、並び替えたシートインデックスを PdfSaveOptions.getSheetSet() オプションに渡すことで実現できます。

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, "sheetset-example.xlsx");
// Open the template excel file
const wb = new AsposeCells.Workbook(filePath);

// Reorder sheets(Sheet1, Sheet2, Sheet3, Sheet4) to sheets(Sheet4, Sheet3, Sheet2, Sheet1)
const sheetSet = new AsposeCells.SheetSet([3, 2, 1, 0]);
const pdfSaveOptions = new AsposeCells.PdfSaveOptions();
pdfSaveOptions.setSheetSet(sheetSet);

// Save the pdf file with PdfSaveOptions
wb.save("output.pdf", pdfSaveOptions);