C++で名前付き範囲を削除

紹介

Excelファイルに多くの定義名や名前付き範囲がある場合、参照されないためにいくつかをクリアする必要があります。

MS Excelで名前付き範囲を削除する

Excelから名前付き範囲を削除するには、次の手順に従うことができます:

  1. Microsoft Excelを開き、名前付き範囲が含まれているワークブックを開きます。
  2. Excelリボンの「数式」タブに移動します。
  3. 「定義された名前」グループの「名前マネージャー」ボタンをクリックします。これにより名前マネージャーのダイアログボックスが開きます。
  4. 名前マネージャーのダイアログボックスで、削除したい名前付き範囲を選択します。
  5. 「削除」ボタンをクリックします。プロンプトが表示されたら削除を確認します。
  6. 「閉じる」ボタンをクリックして名前マネージャーのダイアログボックスを閉じます。
  7. 変更を保存するためにワークブックを保存します。

Aspose.Cells for C++を使用した名前付き範囲の削除

Aspose.Cells for C++を使えば、リスト内のテキストまたはインデックスによって名前付き範囲または定義された名前を削除できます。

#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"Book1.xlsx";

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

    // Instantiate a new Workbook
    Workbook workbook(inputFilePath);

    // Get all the worksheets in the book
    WorksheetCollection worksheets = workbook.GetWorksheets();

    // Delete a named range by text
    worksheets.GetNames().Remove(u"NamedRange");

    // Delete a defined name by index
    worksheets.GetNames().RemoveAt(0);

    // Save the workbook to retain the changes
    workbook.Save(outputFilePath);

    std::cout << "Named ranges removed successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

注意:定義された名前が数式で参照されている場合は削除できません。定義された名前の数式のみ削除可能です。

一部の名前付き範囲を削除する

定義名を削除する際には、ファイル内のすべての数式によって参照されているかどうかを確認する必要があります。 名前付き範囲を削除するパフォーマンスを向上させるために、一部をまとめて削除できます。

#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"Book1.xlsx";

    // Path of output excel file
    U16String outputFilePath = outDir + u"Book2.xlsx";

    // Instantiate a new Workbook
    Workbook workbook(inputFilePath);

    // Get all the worksheets in the book
    WorksheetCollection worksheets = workbook.GetWorksheets();

    // Delete some defined names
    Vector<U16String> namesToRemove = { u"NamedRange1", u"NamedRange2" };
    worksheets.GetNames().Remove(namesToRemove);

    // Save the workbook to retain the changes
    workbook.Save(outputFilePath);

    std::cout << "Named ranges removed successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

重複した定義名を削除する

定義された名前が重複しているためにExcelファイルが壊れることがあります。これらの重複した名前を削除してファイルを修復できます。

#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"Book1.xlsx";

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

    // Instantiate a new Workbook
    Workbook workbook(inputFilePath);

    // Get all the worksheets in the book
    WorksheetCollection worksheets = workbook.GetWorksheets();

    // Delete some defined names
    worksheets.GetNames().RemoveDuplicateNames();

    // Save the workbook to retain the changes
    workbook.Save(outputFilePath);

    std::cout << "Workbook saved successfully after removing duplicate names!" << std::endl;

    Aspose::Cells::Cleanup();
}