行や列のグループ化と解除(C++)

紹介

Microsoft Excelファイルでは、データの概要を作成して、1回のマウスクリックで詳細のレベルを表示したり非表示にしたりできます。

アウトライン記号の1、2、3、+、および-をクリックして、ワークシートのセクションの要約または見出しを迅速に表示したり、個々の要約または見出しの詳細を表示する際に使用できます。下の図で示されているように、個々の要約または見出しの詳細を表示するためにシンボルを使用できます。

行と列のグループ化
todo:image_alt_text

行と列のグループ管理

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

Cellsコレクションには、ワークシート内の行や列を管理するためのいくつかのメソッドが提供されており、そのうちいくつかについて以下で詳しく説明します。

行と列のグループ化

行と列をグループ化するには、CellsコレクションのGroupRowsおよびGroupColumnsメソッドを呼び出すことができます。両方のメソッドは以下のパラメーターを受け取ります:

  • 最初の行/列インデックス、グループ内の最初の行または列
  • グループ内の最後の行/列のインデックス、最後の行または列。
  • 非表示かどうか、グループ化後に行または列を非表示にするかどうかを指定するブールパラメータ。
#include <iostream>
#include <memory>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"book1.xls";

    // Create workbook from file
    Workbook workbook(inputFilePath);

    // Access the first worksheet in the Excel file
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Group first six rows (from 0 to 5) and make them hidden
    worksheet.GetCells().GroupRows(0, 5, true);

    // Group first three columns (from 0 to 2) and make them hidden
    worksheet.GetCells().GroupColumns(0, 2, true);

    // Save the modified Excel file
    U16String outputFilePath = srcDir + u"output.xls";
    workbook.Save(outputFilePath);

    std::cout << "Rows and columns grouped successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

グループ設定

Microsoft Excelでは、以下を表示するためのグループ設定を構成できます:

  • 詳細の下の要約行。
  • 詳細の右側の要約列。

開発者は、WorksheetクラスのGetOutline()プロパティを使用して、これらのグループ設定を構成できます。

詳細の下にサマリー行

サマリー行が詳細の下に表示されるかどうかは、OutlineクラスのGetSummaryRowBelow()プロパティをtrueまたはfalseに設定することで制御できます。

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"sample.xlsx";

    // Path of output Excel file
    U16String outputFilePath = outDir + u"output.xls";

    // Create workbook
    Workbook workbook(inputFilePath);

    // Get the first worksheet
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Grouping first six rows and first three columns
    worksheet.GetCells().GroupRows(0, 5, true);
    worksheet.GetCells().GroupColumns(0, 2, true);

    // Setting SummaryRowBelow property to false
    worksheet.GetOutline().SetSummaryRowBelow(false);

    // Save the modified Excel file
    workbook.Save(outputFilePath);

    std::cout << "Excel file modified and saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

詳細の右側にサマリー列

開発者は、OutlineクラスのGetSummaryColumnRight()プロパティをtrueまたはfalseに設定することで、詳細の右側にサマリー列を表示することもできます。

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"sample.xlsx";

    // Path of output Excel file
    U16String outputFilePath = outDir + u"output.xls";

    // Create workbook
    Workbook workbook(inputFilePath);

    // Get the first worksheet
    WorksheetCollection sheets = workbook.GetWorksheets();
    Worksheet worksheet = sheets.Get(0);

    // Grouping first six rows and first three columns
    worksheet.GetCells().GroupRows(0, 5, true);
    worksheet.GetCells().GroupColumns(0, 2, true);

    // Set summary column to the right
    worksheet.GetOutline().SetSummaryColumnRight(true);

    // Save the modified Excel file
    workbook.Save(outputFilePath);

    std::cout << "Excel file modified and saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

行と列のグループ解除

グループ化された行または列を解除するには、{1}のコレクションの{2}および{3}メソッドを呼び出します。どちらのメソッドも2つのパラメーターを取ります。

  • 最初の行または列のインデックス、グループ化を解除する最初の行/列。
  • 最後の行または列のインデックス、グループ化を解除する最後の行/列。

UngroupRowsには、ブール値の第三パラメーターを取るオーバーロードがあります。これをtrueに設定すると、グループ化された情報がすべて削除されます。それ以外の場合は、外部グループ化情報のみが削除されます。

#include <iostream>
#include <memory>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Path of input Excel file
    U16String inputFilePath = srcDir + u"book1.xls";

    // Create workbook from the input file
    Workbook workbook(inputFilePath);

    // Access the first worksheet in the Excel file
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    // Ungrouping first six rows (from 0 to 5)
    worksheet.GetCells().UngroupRows(0, 5);

    // Ungrouping first three columns (from 0 to 2)
    worksheet.GetCells().UngroupColumns(0, 2);

    // Save the modified Excel file
    U16String outputFilePath = srcDir + u"output.xls";
    workbook.Save(outputFilePath);

    Aspose::Cells::Cleanup();

    return 0;
}