[Solved] GetValue from texbox grid – does this syntax look OK?


QC suggested this syntax yesterday (thank you!) for taking values from the first two cells in the same row in a texbox grid. Does this look OK to you guys, more familiar with custom coding than I am?

%%total = 0;

%%grid = sgapiGetValue(%%271);

%%r1c1 = %%grid{%%row-271}{%%10370};

%%r1c2 = %%grid{%%row-271}{%%10371};

function sgapiAddValues(%%r1c1, %%r1c2) {
%%total = %%r1c1 + %%r1c2;
return %%total;



Thanks a lot!!!

MI asked

    Best answer


    I know it’s been a bit since this was posted, but I thought I’d give my two cents as well.

    Here’s the script I’d use:

    %%row1 = sgapiEvaluateMergeCodes(‘[question(“value”), id=”6″]’); //replace with your row ID
    %%row1 = sgapiExplode(“,”, %%row1); //creates an array from comma separated list of answers

    %%cell1 = %%row1[0]; //get value of first element in array (first cell)
    %%cell2 = %%row1[1]; //get second element

    %%sum = %%cell1 + %%cell2; //add ’em up

    %%output .= %%sum; //output!




    You could add a sgapiSetValue or whatever you need to as well.

    Elizabeth Rose answered
      qc 560 Rep.

      Close, but I think a couple of edits needed. I’m assuming what you ultimately want is the total of Row1/Column1 and Row2/Column2? Try this instead:

      %%grid = sgapiGetValue(271);

      %%r1c1 = %%grid{272}{10370};

      %%r1c2 = %%grid{272}{10371};

      %%total = %%r1c1 + %%r1c2;


      On the above, I’m assuming that your grid is Question ID 271, which would usually make your first row ID 272. You can confirm this by looking at the “Question & Page ID Lookup” dropdown that appears on the Custom Script screen (see screenshot).

      And to clean up the code a bit, no need to initialize your total to 0, since you’re explicitly adding 2 values (not using it as a running total), and no need for the function, unless you’re somehow reusing this script (? not sure about this one, since I haven’t used functions in SG scripting yet).

      Lastly, if the above doesn’t seem to be working, use “272” (with quotes) instead of 272 (without quotes), and use “10370” and “10371” (with quotes) instead of 10370 and 10371 (without quotes). I never remember when you do and don’t need quotes.

      qc answered
        • Thanks a lot for helping me with this code!! Good suggestions. I definitely had the question code wrong, question ID 270, first row 271 from the look-up). Have tried all the variations of the same code (testing after each change), but no luck unfortunately. I am close to giving up on this, but this should work (I was sure it would with the changes you suggested). Thanks again!!



        Question stats

        • Active
        • Views10438 times
        • Answers2 answers
        • Followers0 followers