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}")