ทำความสะอาดก่อนหรือระหว่างการดำเนินการMail Merge

Aspose.Wordsช่วยให้คุณสามารถใช้ตัวเลือกการทำความสะอาดและการลบที่แตกต่างกันเช่นการลบฟิลด์ผสานก่อนที่จะดำเนินการดำเนินการMail Mergeหรือลบพื้นที่ที่ไม่ได้ใช้งานในระหว่างการดำเนินการMail Merge ส่วนนี้จะอธิบายวิธีการลบฟิลด์ที่รวมและวิธีการตั้งค่าตัวเลือกการเอาออก.

ลบเขตข้อมูลที่ผสาน

เมื่อคุณใช้เทมเพลตที่ยาวบางส่วนที่สร้างขึ้นโดยผู้อื่นคุณอาจต้องการลบฟิลด์ผสานทั้งหมดที่มีอยู่แล้วในเทมเพลตนั้นก่อนดำเนินการดำเนินการMail Merge คุณสามารถใช้วิธีการDeleteFieldsถ้าคุณต้องการลบฟิลด์ผสานทั้งหมดออกจากเอกสารโดยไม่ต้องดำเนินการMail Merge เมธอดนี้จะไม่ได้รับผลกระทบจากการลบตัวเลือกใดๆของคุณสมบัติCleanupOptionsและดำเนินการดังกล่า.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีลบฟิลด์ผสานทั้งหมดจากเทมเพลตของคุณโดยไม่ต้องดำเนินการMail Merge:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
// Shows how to delete all merge fields from a document without executing mail merge.
doc.getMailMerge().deleteFields();

ตั้งค่าตัวเลือกRemoving

Aspose.Wordsช่วยให้คุณลบฟิลด์ภูมิภาคและย่อหน้าที่ไม่รวมออกจากเทมเพลตระหว่างการดำเนินการMail Mergeโดยใช้ตัวเลือกการลบ.

ใช้คุณสมบัติCleanupOptionsพร้อมกับการแจงนับMailMergeCleanupOptionsเพื่อตั้งค่าตัวเลือกการลบ ระบุรายการที่คุณต้องการลบโดยการเลือกตัวเลือกต่อไปนี้(คุณสามารถรวมมากกว่าหนึ่ง):

  • ลบย่อหน้าว่าง
  • ลบพื้นที่ที่ไม่ได้ใช้
  • ลบเขตข้อมูลที่ไม่ได้ใช้
  • ลบเขตข้อมูลที่มี
  • ลบเขตข้อมูลแบบคงที่
  • ลบแถวตารางที่ว่างเปล่า

คุณสามารถพิจารณาฟิลด์รวมเป็นไม่รวมในเงื่อนไขใดเงื่อนไขหนึ่งต่อไปนี้:

  1. ถ้าฟิลด์ผสานในแหล่งข้อมูลไม่มีคอลัมน์หรือฟิลด์การแม็พ.
  2. ถ้าฟิลด์ผสานในแหล่งข้อมูลประกอบด้วยฟิลด์การแม็พแต่ข้อมูลเป็นโมฆะ.

ลบย่อหน้าว่าง

ย่อหน้าที่มีเฉพาะฟิลด์ผสานจะว่างเปล่าเมื่อกระบวนการMail Mergeนำฟิลด์ผสานทั้งหมดออกเป็นไม่ผ ย่อหน้าว่างเหล่านั้นสามารถเพิ่มพื้นที่ที่ไม่ต้องการและเปลี่ยนวิธีการสร้างรายงานจะมีลักษ คุณอาจเผชิญกับสองสถานการณ์ที่มีย่อหน้าในระหว่างการดำเนินการMail Merge:

  1. ฟิลด์Mail Mergeจะถูกรวมเข้ากับข้อมูลที่ว่างเปล่า.
  2. ฟิลด์ผสานไม่ได้ใช้และจะถูกลบออก.

ในทั้งสองสถานการณ์ตัวเลือกRemoveEmptyParagraphsจะลบย่อหน้าว่างออกจากเอกสารโดยอัตโนมัติ นอกจากนี้ยังจะลบฟิลด์TableStartและTableEndผสานถ้าส่วนที่เหลือของย่อหน้าว่างเปล่า.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาย่อหน้าว่าง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");

ลบพื้นที่ที่ไม่ได้ใช้

ในรุ่นก่อนหน้าของAspose.Wordsพื้นที่ว่างMail Mergeจะถูกลบออกจากเอกสารโดยอัตโนมัติในระหว่างการดำเนินการMail Merge กับรุ่นล่าสุดของแอสโปสคำว่างเปล่าMail Mergeภูมิภาคยังคงอยู่หลังจากการดำเนินการMail Mergeโดยค่าเริ่มต้น อย่างไรก็ตามคุณสามารถใช้ตัวเลือกRemoveUnusedRegionsเพื่อลบพื้นที่Mail Mergeที่ไม่ได้ใช้ระหว่างการดำเนินการMail Merge ตัวอย่างเช่นคุณสามารถผสานเอกสารกับแหล่งข้อมูลที่ว่างเปล่าที่ประกอบด้วยตารางข้อมู.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเอาพื้นที่ผสานที่ไม่ได้ใช้ออก:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "TestFile Empty.doc");
// Create an empty data source in the form of a DataSet containing no DataTable objects.
DataSet data = new DataSet();
// Enable the MailMergeCleanupOptions.RemoveUnusedRegions option.
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS);
// Merge the data with the document by executing mail merge which will have no effect as there is no data.
// However the regions found in the document will be removed automatically as they are unused.
doc.getMailMerge().executeWithRegions(data);
// Save the output document to disk.
doc.save(dataDir + "TestFile.RemoveEmptyRegions Out.doc");

หมายเหตุ

ลบเขตข้อมูลที่ไม่ได้ใช้

Aspose.Wordsช่วยให้คุณลบฟิลด์ใดๆที่ไม่ได้ใช้Mail MergeโดยกำหนดRemoveUnusedFieldsแฟล็กไปยังCleanupOptions ตัวเลือกนี้จะลบฟิลด์ผสานที่ไม่มีข้อมูลที่สอดคล้องกันในแหล่งข้อมูล.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาเขตข้อมูลที่ไม่ได้ใช้ผสานออกจากเอกสารโดยอัตโนมัติในระหว่างการดำเนินการMail Merge:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");

การลบเขตข้อมูลที่มี

ฟิลด์ผสานสามารถมีอยู่ภายในฟิลด์อื่นเช่นฟิลด์IFหรือฟิลด์สูตร ลบฟิลด์นี้ด้านนอกเมื่อรวมฟิลด์ผสานหรือลบออกจากเอกสาร.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาเขตข้อมูลที่ประกอบด้วยเขตข้อมูลผสานจากเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");

หมายเหตุ

ลบแถวตารางที่ว่างเปล่า

Aspose.Wordsช่วยให้คุณลบแถวตารางที่ว่างเปล่าโดยการกำหนดRemoveEmptyTableRowsธงCleanupOptions ตัวเลือกนี้จะลบแถวตารางที่มีเขตข้อมูลผสานที่ว่างเปล่า.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาแถวตารางว่างที่มีพื้นที่Mail Mergeออกจากเอกสาร:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "RemoveRowfromTable.docx");
doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
doc.save(dataDir + "MailMerge.ExecuteArray_out.doc");