Help

Dynamically assign field names within a script for visible fields in a given view

Topic Labels: Scripting
568 0
cancel
Showing results for 
Search instead for 
Did you mean: 
aaron1
4 - Data Explorer
4 - Data Explorer

Hello,

I have been using Airtable for a little while but have never done any scripting. I am trying to create a script which will make it easy for the rest of my team to update a chart using the view filters. I have included a screen shot to see if that helps me explain it any better. To get the line chart to work as a time series, each supplier had to be a column with one column being contracted and the other supplied. To get it to graph appropriately I have to graph another column which is calculated by using a Running Total script. I then either need to add in these two cumulative columns for each supplier or I was hoping that I could move a supplier into the “Selected Farm to Chart” view and the script would automatically run the Running Total script using the new column names for the supplier currently in the view.

 

For further reference here is the script I am running that I want to add to. I want the “Stag Valley Farm Contracted” and “Stag Valley Farm Supplied” column names to be dynamic when I change the view instead of having to edit the script. Is there a way to do this?

 

// change these names to pick a view:

let table = base.getTable('Imported table');

let view = table.getView('Selected Farm to Chart');

let result1 = await view.selectRecordsAsync({fields: ['Stag Valley Farm Contracted']});

let runningTotal1 = 0;

for (let record of result1.records) {

    // change the field names here to adapt this script to your base

    runningTotal1 += record.getCellValue('Stag Valley Farm Contracted');

    await table.updateRecordAsync(record, {

        'Cumulative Contracted': runningTotal1,

    });

}

let result2 = await view.selectRecordsAsync({fields: ['Stag Valley Farm Supplied']});

let runningTotal2 = 0;

for (let record of result2.records) {

    // change the field names here to adapt this script to your base

    runningTotal2 += record.getCellValue('Stag Valley Farm Supplied');

    await table.updateRecordAsync(record, {

        'Cumulative Supplied': runningTotal2,

    });

}

 

 

 

aaron1_0-1687399013128.png

 

0 Replies 0