notion-py
notion-py copied to clipboard
Choose the order of column properties of a table created from the API
I'm trying to create some tables from a python script, however, whether I create it with specifying its schema or copying it from a pre-existing table (already created manually on notion), I can't choose the order of the columns. The columns of tables newly created or copied via the API are always automatically arranged in alphabetical order.
PS: I'm talking about the column order, not the row.
Thanks for all kind of help!
I ran into this problem too, so I looked at what Notion did in the browser when changing the order of the columns. When sending an update of the order, it sends an transaction that, translated to python, looks like this:
client.submit_transaction(
build_operation(
id=collection_view_id,
table="collection_view",
path=[], # i don't know what this one is for
command='update',
args={
'format': {
'table_properties': table_properties_
}
}
)
)
table_properties_
is a list containing dictionaries of the properties, in order of desired column order.
That was probably too vague so i'll try to clarify with an example:
Let's suppose that we have three columns, that we want in the order: ['c2', 'c1', 'c3']
, these are the schema names of the columns.
table_properties_ = [
# Using list comprehension because it's easier here
{'visible': True, 'property': schema_id} for schema_id in ['c2', 'c1', 'c3']
]
'visible': True
and 'property'
are the only properties necessary for this to work. You can also specify column-width ('width'
) in this request. (There are probably more extra properties but I haven't found them yet.)
Thanks, it has helped much!
The creation added in #274, but to sort and changed 'visible', maybe later.
Thanks, but I have this issue when launching the code
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
<ipython-input-276-e512b645186a> in <module>
10 'table_properties': [
11 # Using list comprehension because it's easier here
---> 12 {'visible': True, 'property': schema_id} for schema_id in ['control_reference',
13 'date_of_execution',
14 'testor',
~/venv/lib/python3.7/site-packages/notion/client.py in submit_transaction(self, operations, update_last_edited)
288 else:
289 data = {"operations": operations}
--> 290 self.post("submitTransaction", data)
291 self._store.run_local_operations(operations)
292
~/venv/lib/python3.7/site-packages/notion/client.py in post(self, endpoint, data)
260 raise HTTPError(
261 response.json().get(
--> 262 "message", "There was an error (400) submitting the request."
263 )
264 )
HTTPError: Unsaved transactions: Space not found in record path