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

Here is the full solution for how to iterate over an array using JavaScript. Smartsheet provides a partial and incorrect solution at
. 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
-
@Genevieve P. , could you have someone correct , please? If that is not possible, could you link to this post from that article to make it easier on future Bridge JavaScript learners?
Thank you!
Neil -
Thank you for providing your solution with such detailed steps! I've moved this over to the Best Practice topic to make it easier to find. I will also pass this along to the Help Center team for their review.
Cheers,
GenevieveNeed more information? 👀 | Help and Learning Center
こんにちは (Konnichiwa), Hallo, Hola, Bonjour, Olá, Ciao!👋 | Global Discussions
Categories
- All Categories
- 14 Welcome to the Community
- Customer Resources
- 66.4K Get Help
- 430 Global Discussions
- 152 Industry Talk
- 492 Announcements
- 5.2K Ideas & Feature Requests
- 85 Brandfolder
- 155 Just for fun
- 76 Community Job Board
- 504 Show & Tell
- 33 Member Spotlight
- 2 SmartStories
- 307 Events
- 37 Webinars
- 7.3K Forum Archives