Home>

I want to use GAS to display numerical values ​​arranged like red vertically in the blue area.

Error message

Like the GIF movie, the acquisition of the last line in the loop does not reflect the previous processing, and the result is not reflected well.

Error message
`` `

Applicable source code

Source code

function getData () {
var sht = SpreadsheetApp.getActive (). getSheetByName ('test');

for (var i = 1;i<3;i ++)
{
const Values ​​= sht.getRange (1,4,10,1) .getValues ​​();
const Lastcont = Values.filter (String) .length;
sht.getRange (1, i, 10, 1) .copyTo (sht.getRange (Lastcont + 1,4,10,2), {contentsOnly: true});

}
}

I tried flush, but it didn't work.

Supplemental information (FW/tool version etc.)

Only the problematic parts are extracted and posted.

  • Answer # 1

    The cause of the problem seems to beconst. If you are modifying a script that you are using, try modifying it as follows:

    From:
    const Values ​​= sht.getRange (1,4,10,1) .getValues ​​();
    const Lastcont = Values.filter (String) .length;
    To:
    var Values ​​= sht.getRange (1,4,10,1) .getValues ​​();
    var Lastcont = Values.filter (String) .length;

    This changesValues ​​,Lastcont, socopyToseems to add a value.

    Supplement

    Be careful when usingconstin Google Apps Script. For example, if you run the following script

    for (var i = 0;i<5;i ++) {
      const n1 = i;
      var n2 = i;
      Logger.log ("% s,% s", n1, n2)
    }

    The following result is returned. If it is Javascript, bothn1andn2will give0, 1, 2, 3, 4Apps Script seems different.

    0.0, 0.0
    0.0, 1.0
    0.0, 2.0
    0.0, 3.0
    0.0, 4.0

Related articles