Best Practice

Best Practice

Review best practices, tips and tricks, and more from fellow Community members.

Smartsheet Bridge: Iterate over an array using JavaScript - Column names from a sheet

✭✭✭✭✭✭
edited 01/28/25 in Best Practice

Here is the full solution for how to iterate over an array using JavaScript. Smartsheet provides a partial and incorrect solution at Site faviconJavaScript module reference | Smartsheet Learning Center

. Below is the actual solution.

Using Bridge and JavaScript, this example extracts the list of column names from a sheet and provides them as the only elements in an array. The JavaScript code analyzes all elements of a larger array to create the smaller column name array.

1. Get the sheetID from the sheet properties.


2. Add the "Smartsheet: List Columns" module and put YourSheetID under "Sheet ID".

3. Add the "JavaScript: Run Script" module

4. Create the JavaScript.
Click the gear.


5. Expand "Scripts" and "Script 1". Name your script in the "Script Name field". I used "TestAdd".
Add the code below in the Script Body field.
There are two variables that you will define in the "JavaScript: Run Script" Module", "input" and "field".
Click save.

Code
function getFields(input, field) {
var output = [];
for (var i=0; i < input.length ; ++i)
{
//print only one column
output.push(input[i][field]);
}
return output;
}
return getFields(input, field);

7. Save the WorkFlow.
6. Get the values for the "input" and "field" variables.
Click the "run log" icon and then the "run" button.


7. Wait and click the refresh button.


8. When run is complete it will have a "stopped" status because you have not defined the variables in the "JavaScript: Run Script" module. Click the stopped run.


8. Expand the "Smartsheet: List Columns". Click the three dots beside data: Array[#] and click "Copy Data Reference". Paste this on a notepad for future reference. The result should look like:
{{states.startstate.smartsheet.list_columns.data}}

Copy the field name beside the array item you want. In this case it is "title". Paste it in the notepad for future reference.


9. Complete the "JavaScript: Run Script" module.
Put your script name in the "Script Name" field. In my case it is "TestAdd".
Expand "Script Parameters" and enter these values.
Key #1 = input
Value #1 = Data Reference from above. Mine is {{states.startstate.smartsheet.list_columns.data}}
Key #2 = field
Value #2 = Array field you want to pull. Mine is title

10. Save the WorkFlow


11. Run the WorkFlow.


12. Click refresh until the run is complete.
13. Click the completed run.


14. Expand "JavaScript: Run Script"

The columns are listed as the only items in the Array.


Neil Egsgard
Business Solutions Architect
Southern Alberta Institute of Technology

Comments

Other Best Practices