I am currently trying to import configuration data into Grist. This data is processed into a CSV file in a previous step, and the file is already named after the Grist table into which it will be imported. Almost all of the data types work fine, except for the (choice) list. The list doesn’t get split up and is marked red upon import. I expected it to detect the individual tags and display them separately. While I can manually split these values using formulas, I won’t be the one handling the import in the future, so it would be great to skip this step.
Is there a way to format data inside a CSV so that it gets recognized as a choice list? If not, is it possible to achieve this by switching to a different datatype? Thank you!
I suspect the choice list gets imported as a simple string, commas and all. In that case you’d have to process that string and turn it into a proper list using a formula. Here’s a quick suggestion:
Add a trigger formula to the choice column, set to trigger whenever the column itself changes. Put the following formula:
from grist import AltText
import re
# When Grist doesn't understand a given value, it will put an AltText object into the cell instead. That's what makes the cell go red. So first off, let's make sure this formula only does its magic if we have an AltText, and ignore any other values.
if isinstance(value, AltText):
# This object will contain the value as a simple string, which we can obtain by casting to str:
value = str(value)
# We'll now remove the "[" and "]" at either ends of the string:
value = value.strip("][")
# Finally, let's split the string by comma (ignoring how much whitespace there is after the comma), using a regular expression. The result will be a list of strings, which is exactly what the choice list column requires.
value = re.split(r",\s*", value)
# Finally, spit out the value so it gets placed into the cell.
return value
I was facing the same difficulty.
Haven’t tried importing a CSV file, but copying from CSV and pasting to Grist, the following data works:
tag1,tag2
tag1
tag3, tag2
tag4 ,tag1
multi-word tag
Pasting this into a new column, then switching its type to Choice List will create a valid choice for each tag. Zero or more spaces on either side of the comma works fine.