Use an object as a parameter 🎖

I’m working with a formula that expects a custom object as a parameter.
The object was defined in this way:

const FancyObject = coda.makeObjectSchema({
  properties: {
    PropertyOne: { type: coda.ValueType.String },
    PropertyTwo: { type: coda.ValueType.Number },
    PropertyThree: { type: coda.ValueType.Number },
  },
});

Then, the formula is coded like this:

coda.makeParameter({
      type: coda.ParameterType.FancyObject,
      name: "CoolParameter",
      description: "This is a cool parameter in a formula",
    }),

I’m getting an error in the line type: coda.ParameterType.FancyObject, which doesn’t surprise me, but I can’t handle to set it properly. Basically, I need my parameter to accept data in the form of an object.

Hi @Giovanni_Sades - it isn’t terrible clear in the docs at the moment, but you can’t use an object as a parameter. The list of supported parameter types is here: Enumeration: ParameterType - Coda Pack SDK

In some cases you can just have your formula accept multiple parameters, that correspond to the fields of the object.

1 Like

I hope someday parameters support objects. In the meantime, is it possible ho have a formula working with objects?
For example, a “Product” column contains a custom Product object which has a weight property. A second column, “Package”, contains a custom Package object which also has a weight property. A finally column, “Shipping” contains a custom Shipping object and its properties are calculated from the Product and Package columns; one of them is the total weight. Is it possible to make a formula to handle this?
I know that in this simplification, it’s easy to solve without packs, but is just an example. Maybe you can point me in the right direction.

Yes, these sorts of use cases should be fairly straightforward. Just have your formula accept whatever parameters it needs (weights I’m this case) and pass them in from your objects. Something like:

CalculateShipping(Product.Weight, Package.Weight)

This is how all started. How can I do this?

You look at some samples with parameters here:

https://coda.github.io/packs-sdk/samples/topic/parameter/

Thanks for the follow-up Eric, I really appreciate it.
I can’t find any information that could give me a hint of what can I do differently.

I’m defining the schema, and the column format:

import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

const ElementGroupSchema = coda.makeObjectSchema({
  type: coda.ValueType.Object,
  properties: {
    representation: {
      description: "Group details represented in a string format.",
      type: coda.ValueType.String
    },
    firstComponent: {
      description: "Value of the first component of the group.",
      type: coda.ValueType.Number
    },
    secondComponent: {
      description: "Value of the second component of the group.",
      type: coda.ValueType.Number
    },
  },
});

pack.addColumnFormat({
  name: "Grouped emelents",
  instructions: "Grouped elements in the proper format.",
  formulaName: "ToGrouped",
  formulaNamespace: "Deprecated",
});

Then, I made a formula to create the group from text entered in a column and generate the object. The formula is working nicely:

pack.addFormula({
  resultType: coda.ValueType.String,
  name: "ToGrouped",
  description: "Formats a grouped set.",
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "group",
      description: "The string with the elemets to form a group.",
    }),
  ],
  execute: async function ([group]) {
    // ...
  },
});

In the table, there are many columns of this type (Grouped elements). There is also a column that processes these columns. For instance, a Result column takes the grouped elements from two columns and generates a new objet to place in itself.

pack.addFormula({
  name: "GroupProcess",
  description: "Generates a new group from the paraeters of two other groups.",
  parameters: [
    coda.makeParameter({
      name: "firstGroup",
      type: //yet to solve
      description: "The first group to operate.",
    }),
    coda.makeParameter({
      name: "secondGroup",
      type: //yet to solve
      description: "The second group to operate.",
    }),
  ],
  resultType: coda.ValueType.Object,
  schema: ElementGroupSchema,

  execute: async function ([firstGroup, secondGroup], context) {
    //...
  }
});

This is not working, since I haven’t found a way to give the formula access to the right type.

The issue here is that I can’t figure our how to have the formula accept the parameter it needs (a member of the ElementGroupSchema, in this case).

I reviewed the available documentation again hoping to find something that is hiding to my eye. Since packs as new stuff, there is no a similar thread in the community forum also.

1 Like

In cases like this you need to break your schema apart, and then pass the pieces individually. So for example, you could have 6 parameters:

  • String firstGroupRepresntation
  • Number firstGroupFirstComponent
  • Number firstGroupSecondComponent
  • String secondGroupRepresntation
  • Number secondGroupFirstComponent
  • Number secondGroupSecondComponent

And you’d call the function like

GroupProcess(Column1.Representation, Column1.FirstComponent, Column1.SecondComponent, Column2.Representation, Column2.FirstComponent, Column2.SecondComponent)

That said, if you don’t need all three pieces of each schema (for instance, you could get all the information you need from the string representation) then just pass piece of data.

2 Likes

Well, I wanted to pass the complete object, but since it’s not possible, this is the best option. It works nicely. Thank you very much, Eric.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.