1

How to delete a column with conditional formatting rule using smartsheet C# SDK?

We are getting error -

"The column specified is used in a conditional formatting rule, so the column cannot be deleted and its type cannot be changed."

.

piet.t
  • 11,718
  • 21
  • 43
  • 52

2 Answers2

2

@Mohamed_Shahrestani is correct that any conditional format rule(s) that are applied to the column must be deleted before the column can be deleted. Unfortunately, it seems that the Smartsheet API does not currently support managing conditional format rules -- so it's not currently possible to achieve this via API. The only way you'd be able to delete a column that has conditional format rules applied would be to first manually (via the Smartsheet UI) remove all conditional format rules from the column.

Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
0

You should delete column's conditional format first then delete your column. Meaning : cause of error will be gone.

myRange is your column's conditional formatts.

Tip1 : You need to delete all of conditions so you don't need if statement.

foreach (Microsoft.Office.Interop.Excel.FormatCondition fc in myRange.FormatConditions)
{
  if (fc.Formula1 == whatever) // You don't need this if statement. bcz you want to delete all of them
  {
    fc.Delete();
  }
}

Look at this question for samples.

@Siddhart Rout 's answer :

private void btnSearch_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Excel.Application xlexcel;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

    xlexcel = new Excel.Application();
    xlexcel.Visible = true;

    //~~> Add a File
    xlWorkBook = xlexcel.Workbooks.Add();
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    //~~> Create 2 Conditions
    xlWorkSheet.Cells[1, 1].FormatConditions.Add( 1,5,"=5");
    xlWorkSheet.Cells[1, 1].FormatConditions.Add(1, 5, "=10");

    MessageBox.Show("Wait");
    //~~> Now if you check the Excel file, Cell A1 has two conditional formats.
    //~~> See Snapshot 1

    //~~> Delete the first condition
    xlWorkSheet.Cells[1, 1].formatconditions(1).delete();

    MessageBox.Show("Wait");
    //~~> Now if you check the Excel file, Cell A1 has only 1 conditional format.
    //~~> See Snapshot 2
}

I hope this helps you.