Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created February 8, 2024 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/96a5a590551dcdbd15ca58656192a90e to your computer and use it in GitHub Desktop.
Save bjoerntx/96a5a590551dcdbd15ca58656192a90e to your computer and use it in GitHub Desktop.
using TXTextControl.DocumentServer.Fields;
namespace TXTextControl.DocumentServer.DataSources
{
public static class Refactor
{
public static void RenameDataColumn(TextControl textControl,
string oldColumnName,
string newColumnName)
{
// iterate through all TextParts
foreach (TextPartBase textPart in textControl.TextParts)
{
RenameMergeBlocks(textPart.SubTextParts, oldColumnName, newColumnName);
RenameMergeFields(textPart.ApplicationFields, oldColumnName, newColumnName);
}
}
private static void RenameMergeBlocks(SubTextPartCollection subTextParts,
string oldColumnName,
string newColumnName)
{
// iterate through all SubTextParts
foreach (SubTextPart subTextPart in subTextParts)
{
// check if the SubTextPart is a merge block
if (subTextPart.Name == "txmb_" + oldColumnName)
{
// rename the SubTextPart
subTextPart.Name = "txmb_" + newColumnName;
}
}
}
private static void RenameMergeFields(ApplicationFieldCollection applicationFields,
string oldColumnName,
string newColumnName)
{
// iterate through all ApplicationFields
foreach (ApplicationField applicationField in applicationFields)
{
// check if the ApplicationField is a merge field and contains the old column name
if (applicationField.TypeName == "MERGEFIELD" &&
applicationField.Name.Contains(oldColumnName))
{
// create a new MergeField object and replace the old column name with the new column name
MergeField mergeField = new MergeField(applicationField);
mergeField.Name = mergeField.Name.Replace(oldColumnName, newColumnName);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment