Deleting Columns using API and python

Bobert
Bobert ✭✭✭✭

I have a column that is identical on hundreds of sheets. I am attempting to delete the column using python and the API. I am currently just trying to delete it from one sheet using a script as a proof of concept for all of the sheets that have it. The column in question is a date column with a column formula. My understanding is that I have to get the column id, convert the column formula to cell formulas, then update the cells to remove the value and formula, and then I can finally delete this column. I keep getting a general error, Sheets.delete_column() missing 1 required positional argument: 'column_id' . I am not sure what this is referencing. I believe it has something to do with my final call to delete the column. I can't find any information about the arguments needed for the client.Sheets.delete_column(column_id) line. I added the function below that is supposed to delete the column. Any advice on how to proceed would be great. Thanks. (note: Indentation is correct on actual code, copying messed it up.)

def delete_due_on_site_column(sheet_id):
try:
# Step 1: Get the column ID
sheet = client.Sheets.get_sheet(sheet_id)

    column_title = "Due On Site"

column_id = None
for col in sheet.columns:
if col.title == column_title:
column_id = col.id
print(column_id)
break

if column_id is None:
print(f"Column with title '{column_title}' not found.")
return

# Step 2: Remove the formula (if any)
column_update = smartsheet.models.Column({
'id': column_id,
'formula': None
})
client.Sheets.update_column(sheet_id, column_id, column_update)

# Step 3: Update cells in the column to clear values and formulas
rows_to_update = []
for row in sheet.rows:
for cell in row.cells:
if cell.column_id == column_id:
cell.value = ""
cell.formula = None
update_cell = smartsheet.models.Cell({
'column_id': column_id,
'value': "",
'formula': None
})
update_row = smartsheet.models.Row({
'id': row.id,
'cells': [update_cell]
})
rows_to_update.append(update_row)

if rows_to_update:
client.Sheets.update_rows(sheet_id, rows_to_update)

# Step 4: Delete the column
client.Sheets.delete_column(column_id)
print(f"Deleted column with title: '{column_title}'")

except smartsheet.exceptions.SmartsheetException as e:
print(f"Smartsheet API error: {e}")
except Exception as e:
print(f"General error: {e}")

Answers

  • Brian_Richardson
    Brian_Richardson Overachievers Alumni

    You don't have to go through all those steps to delete the column, just the simple delete. You don't have to remove the column formulas etc. You will, however, need the column id.

    Easiest way to find the column ID is iterate through your list of sheet IDs and do List Columns, then find the matching column title that you want in the resulting list of columns and get the id from the same column object.

    https://smartsheet.redoc.ly/tag/columns#operation/columns-listOnSheet

    response = smartsheet_client.Sheets.get_columns(
      9283173393803140,       # sheet_id
      include_all=True)
    columns = response.data
    

    {
    "id": 0
    ,
    "index": 0,
    "symbol": "string",

    "title": "string"
    ,
    "type": "ABSTRACT_DATETIME",
    "validation": true
    }

    Then do a quick and simple column delete using that id for the sheet and id for the column that you found.

    https://smartsheet.redoc.ly/tag/columns#operation/column-delete

    smartsheet_client.Sheets.delete_column(
      9283173393803140,       # sheet_id
      0123456789012345)       # column_id
    

    BRIAN RICHARDSON | PMO TOOLS AND RESOURCES | HE|HIM

    SEATTLE WA, USA

    IRON MOUNTAIN