I'm trying to set up a value table to accept multiple parameter data types as input. It's possible for non-value table parameters; from the documentation :
def getParameterInfo(self):
#Define parameter definitions
# First parameter
param0 = arcpy.Parameter(
displayName="Input Raster Dataset",
name="in_rasterdataset",
datatype=["DERasterDataset", "DERasterCatalog"],
parameterType="Required",
direction="Input")
However, when I try on value tables, I get the following error.
param2 = arcpy.Parameter(displayName='Input Data',
name='InputData',
datatype="GPValueTable",
parameterType='Required',
direction='Input')
dTypes = ["DEFeatureClass", "DEFeatureDataset", "DETable",
"DEMosaicDataset", "DERasterDataset"]
param2.columns = [[dTypes, "Old"], [dTypes, "New"]]
Hmm, ok. Maybe I'm just doing it wrong? Let me try making it a single parameter type
param2 = arcpy.Parameter(displayName='Input Data',
name='InputData',
datatype="GPValueTable",
parameterType='Required',
direction='Input')
param2.columns = [["DEFeatureClass", "Old"], ["DEFeatureClass", "New"]]
Bingo:
So, my question is: How can I make a value table parameter accept multiple data types?
(Also, before anyone suggests it, I did already try
param2.columns = [[["DEFeatureClass", "DETable"], "Old"],
["DEFeatureClass", "DETable"], "New"]
]
with the same results.
I would do the type checking in the validation function instead of the initialization. You can just pass "GPType" as the datatype and let it take any input then immediately set an error message on the parameter if the datatype is invalid. Something like this:
import arcpy
param2 = arcpy.Parameter(
displayName='Input Data',
name='InputData',
datatype="GPValueTable",
parameterType='Required',
direction='Input')
dTypes = [
"DEFeatureClass",
"DEFeatureDataset",
"DETable",
"DEMosaicDataset",
"DERasterDataset",
]
# Use generic type by
param2.columns = [["GPType", "Old"], ["GPType", "New"]]
# This goes wherever your validation function is
def validate_gp_table_types(param: arcpy.Parameter, allowed_types: list):
for row in param.value:
if arcpy.Describe(row[0]).dataType not in allowed_types:
param.setErrorMessage(f"{row[0]} is not a valid data type {allowed_types} required")
return
You can also try filtering after you create the column, this is untested though so just take it as more of an idea.
import arcpy
param2 = arcpy.Parameter(
displayName='Input Data',
name='InputData',
datatype="GPValueTable",
parameterType='Required',
direction='Input')
dTypes = \
[
"DEFeatureClass",
"DEFeatureDataset",
"DETable",
"DEMosaicDataset",
"DERasterDataset",
]
param2.columns = \
[
["GPType", "Old"],
["GPType", "New"]
]
for filter in param2.filters:
filter.type = "ValueList"
filter.list = dTypes
params = [param2]