ทำความสะอาดก่อนหรือระหว่างการดำเนินการ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-.NET.git. | |
Document doc = new Document(); | |
doc.MailMerge.DeleteFields(); |
ตั้งค่าตัวเลือกRemoving
Aspose.Wordsช่วยให้คุณลบฟิลด์ภูมิภาคและย่อหน้าที่ไม่รวมออกจากเทมเพลตระหว่างการดำเนินการMail Mergeโดยใช้ตัวเลือกการลบ.
ใช้คุณสมบัติCleanupOptionsพร้อมกับการแจงนับMailMergeCleanupOptionsเพื่อตั้งค่าตัวเลือกการลบ ระบุรายการที่คุณต้องการลบโดยการเลือกตัวเลือกต่อไปนี้(คุณสามารถรวมมากกว่าหนึ่ง):
- ลบย่อหน้าว่าง
- ลบพื้นที่ที่ไม่ได้ใช้
- ลบเขตข้อมูลที่ไม่ได้ใช้
- ลบเขตข้อมูลที่มี
- ลบเขตข้อมูลแบบคงที่
- ลบแถวตารางที่ว่างเปล่า
คุณสามารถพิจารณาฟิลด์รวมเป็นไม่รวมในเงื่อนไขใดเงื่อนไขหนึ่งต่อไปนี้:
- ถ้าฟิลด์ผสานในแหล่งข้อมูลไม่มีคอลัมน์หรือฟิลด์การแม็พ.
- ถ้าฟิลด์ผสานในแหล่งข้อมูลประกอบด้วยฟิลด์การแม็พแต่ข้อมูลเป็นโมฆะ.
ลบย่อหน้าว่าง
ย่อหน้าที่มีเฉพาะฟิลด์ผสานจะว่างเปล่าเมื่อกระบวนการMail Mergeนำฟิลด์ผสานทั้งหมดออกเป็นไม่ผ ย่อหน้าว่างเหล่านั้นสามารถเพิ่มพื้นที่ที่ไม่ต้องการและเปลี่ยนวิธีการสร้างรายงานจะมีลักษ คุณอาจเผชิญกับสองสถานการณ์ที่มีย่อหน้าในระหว่างการดำเนินการMail Merge:
- ฟิลด์Mail Mergeจะถูกรวมเข้ากับข้อมูลที่ว่างเปล่า.
- ฟิลด์ผสานไม่ได้ใช้และจะถูกลบออก.
ในทั้งสองสถานการณ์ตัวเลือกRemoveEmptyParagraphsจะลบย่อหน้าว่างออกจากเอกสารโดยอัตโนมัติ นอกจากนี้ยังจะลบฟิลด์TableStart
และTableEndผสานถ้าส่วนที่เหลือของย่อหน้าว่างเปล่า.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาย่อหน้าว่าง:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table with fields.docx"); | |
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs; | |
doc.MailMerge.Execute(new string[] { "FullName", "Company", "Address", "Address2", "City" }, | |
new object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" }); | |
doc.Save(ArtifactsDir + "WorkingWithCleanupOptions.RemoveEmptyParagraphs.docx"); |
ลบพื้นที่ที่ไม่ได้ใช้
ในรุ่นก่อนหน้าของ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-.NET.git. | |
Document doc = new Document(MyDir + "Mail merge destination - Northwind suppliers.docx"); | |
DataSet data = new DataSet(); | |
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions; | |
// doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields; | |
// doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveStaticFields; | |
// doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveEmptyParagraphs; | |
// doc.MailMerge.CleanupOptions |= MailMergeCleanupOptions.RemoveUnusedFields; | |
// 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.MailMerge.ExecuteWithRegions(data); | |
doc.Save(ArtifactsDir + "WorkingWithCleanupOptions.RemoveUnmergedRegions.docx"); |
หมายเหตุ
ลบเขตข้อมูลที่ไม่ได้ใช้
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-.NET.git. | |
Document doc = new Document(MyDir + "Table with fields.docx"); | |
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields; | |
doc.MailMerge.Execute(new string[] { "FullName", "Company", "Address", "Address2", "City" }, | |
new object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" }); | |
doc.Save(ArtifactsDir + "WorkingWithCleanupOptions.RemoveUnusedFields.docx"); |
การลบเขตข้อมูลที่มี
ฟิลด์ผสานสามารถมีอยู่ภายในฟิลด์อื่นเช่นฟิลด์IFหรือฟิลด์สูตร ลบฟิลด์นี้ด้านนอกเมื่อรวมฟิลด์ผสานหรือลบออกจากเอกสาร.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาเขตข้อมูลที่ประกอบด้วยเขตข้อมูลผสานจากเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table with fields.docx"); | |
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields; | |
doc.MailMerge.Execute(new string[] { "FullName", "Company", "Address", "Address2", "City" }, | |
new object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" }); | |
doc.Save(ArtifactsDir + "WorkingWithCleanupOptions.RemoveContainingFields.docx"); |
หมายเหตุ
ลบแถวตารางที่ว่างเปล่า
Aspose.Wordsช่วยให้คุณลบแถวตารางที่ว่างเปล่าโดยการกำหนดRemoveEmptyTableRowsธงCleanupOptions ตัวเลือกนี้จะลบแถวตารางที่มีเขตข้อมูลผสานที่ว่างเปล่า.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเอาแถวตารางว่างที่มีพื้นที่Mail Mergeออกจากเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Table with fields.docx"); | |
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyTableRows; | |
doc.MailMerge.Execute(new string[] { "FullName", "Company", "Address", "Address2", "City" }, | |
new object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" }); | |
doc.Save(ArtifactsDir + "WorkingWithCleanupOptions.RemoveEmptyTableRows.docx"); |