Search code examples
csvmacrosimacros

Need solution regarding imacros !DATASOURCE_LINE issue


I have a csv file containing info for tv-series and each tv-series has its own macro. I can get all info without any problems using {{!COL1}} etc. But I need some help on the scenario below.

tv-series.csv

Game of Thrones, 2011, Season 2
The Walking Dead, 2015, Season 5
etc.

If I want to get the title of a tv-series I will simply use {{!COL1}}, and for year {{!COL2}}

But I sometimes add new tv-series and the order of them compeletly changes in csv file. Then I have to modify all line #s in all macros manually.

Is there a way to determine the line number based on the firt column (e.g. Game of Thrones)? The info will not change, just the line numbers of them will change. Sorry for my terrible English, I hope I could tell my problem and need some advice.

My code so far

VERSION BUILD=8910303 RECORDER=FX
TAB T=1
SET !DATASOURCE tv-series.csv
SET !DATASOURCE_LINE 2 => This # should change based on first column

TAG POS=1 TYPE=TEXTFIELD FORM=NAME:title ATTR=ID:titlebox CONTENT={{!COL1}}

Solution

  • Use the command 'EVAL' and 'switch'-clause. As an example:

    ...
    SET !DATASOURCE tv-series.csv
    SET !DATASOURCE_LINE 1
    SET lineNumber EVAL("switch ('{{!COL1}}') {case 'Game of Thrones': s = 2; break; case 'Something Else': s = 5; break;  /* etc. */}")
    SET !DATASOURCE_LINE {{lineNumber}}
    ...