1

I'm very new to JavaScript, and have only recently started fiddling with coding.

I had found a question that is similar to what I'm asking, so I've tried to modify the suggestions to fit my needs, but am drawing a blank pulling together other resources to understand the solution. That question and answer is here: range.getValues() With specific Date in Specific Cell

I hope to ask my question by adding it to that thread, but I'm unable to make a comment - only an Answer, as I have zero votes.

I want to know how I can use the range.setValues() function, but specify only rows of cells that fit a certain criteria (e.g. cell in column B = 1).

Mostly I understand the answer, I think - push the full list of data into another array, then use a for loop to identify which rows to "keep".

However, I don't understand a good chunk of the solution, specifically this portion:

data.forEach(function(row) {
  var colHvalue = row[7];
  var colKvalue = row[10];
  if( /* your desired test */) {
    // Add the name of the sheet as the row's 1st column value.
    row.unshift(sheetName);
    // Keep this row
    kept.push(row);
  }
});

where I'm confused:

  1. What's function(row) refer to? It seems to me that "row" is not a defined variable, and is also not a method.

  2. What are the objects being specified in the line var colHvalue = row[7];? And same for the second row.

  3. How does the pushing and unshifting work?

Hope for some answers, thanks!

Edit:

So I still don't understand the detailed logic of the Answer to that other question, and wrote something else based off that:

 function importtest() {



  var sheetX = SpreadsheetApp.openById("XXX-XXX-XXX"); // workbook containing orginal data
  var tabX = sheetX.getSheetByName("EXPORTER"); // tab containing original data
  var rangeX = tabX.getRange(6,2,sheetX.getLastRow(),sheetX.getLastColumn()).getValues(); //range of full original data



var kept = [] ;
var data = rangeX ; //storing full data in "data"

// define function "latestrows", which fills up the "kept" array with selected rows
function latestrows() {
  var datesent = data[i][2] ; // indicating dynamic cell position (using variable i)  that determines if should be kept or not
  var datarow = data[i] ; // specify the entire row of data
  // loop statement; starting with i=0 (or row 1), and increasing until the last row of "data"
  for ( i = 0; i < data.length ; i++) {
    if (datesent == 1) { // if cell in the col indicating if it should be kept, is "1"
      kept.push(datarow); //move the chosen rows of data into "kept"
    }
  }
}

  var sheetX3 = SpreadsheetApp.openById("XXX-XX-XX"); // workbook import destination
  var tabX3 = sheetX3.getSheetByName("IMPORTED"); // import destination tab
  var rangeX3 = tabX3.getRange(7,3,kept.length,kept[0].length) // imported array range


  rangeX3.setValues(kept); // set values
 }

I get the error message TypeError: Cannot read property "length" from undefined. (line 31, file "Code"), which together with testing other modifications indicates to me that kept is empty. What am I missing?

Tea
  • 21
  • 6
  • Why don't you take a [tutorial](https://www.w3schools.com/js/js_arrays.asp) ? – TheMaster Jan 30 '19 at 10:04
  • Thanks for responding! I have taken tutorials but as I've said, I'm really new to coding generally. The tutorials I've taken have helped me to understand parts of this, but not all of it. I know what a variable is, and what functions are, and how to define/construct them generally. But in the cases above, for example, "row" appears to be the name given to the row number or something, but how does that work? Row[7] would mean the 8th object in the list row, but why? I'm also able to find what the `unshift` and `push` methods do, but how does that work to help identify the rows to keep? – Tea Jan 30 '19 at 17:52
  • 1
    Did you read the next page and how [`.forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) works? – TheMaster Jan 30 '19 at 18:49
  • Yes, I had. With your suggestion I'd gone over and looked at them again - you're right that I don't understand how `forEach()` works. – Tea Jan 31 '19 at 08:47
  • So `array.forEach(function(currentValue, index, arr), thisValue)` -> what does "current value of the current element" mean? I get that it may be (I'm not sure if it is) similar to `i` to indicate the ith time of an iteration, but I'm not able to expand on that inkling to an actual example. As for `push` and `unshift` - I know what they do individually. `Push` would be adding the identified rows into the new/temporary array. But it's being used together with `unshift` - for which I know its function individually, but how does it help in selecting/acting on the rows? – Tea Jan 31 '19 at 09:02
  • 1
    So, getValues() returns a 2 dimensional array. `[[],[],[]...]` `forEach()` iterates the elements of outer array, which is the inner array(each row). Read [this](https://developers.google.com/sheets/api/samples/reading) and [this](https://stackoverflow.com/a/51515924) (espescially all the reference links) So each ``row`` in forEach will be the inner array or each row, which is being modified with `push`/`unshift`. – TheMaster Jan 31 '19 at 09:20
  • `var datesent = data[i][2] ;` But there's no `i` at this stage. – TheMaster Jan 31 '19 at 09:25

0 Answers0