Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between getRange, getDataRange, and getActiveRange?

If it isn't immediately apparent by the question, I am pretty new to Google Apps Script. I'm trying to write a Spreadsheets function that runs through each row of a form submission sheet in a workbook and update a second sheet/range (in the same workbook) based on the values of the cells in the first using a for loop. However, nothing happens when I run the function.

I'm pretty sure the error is in how I'm defining the ranges in question, but I'm not 100% sure. Here's a modified version of what I've written:

function update() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var secondSheet = ss.getSheets()[1];
  var submissionSheet = ss.getSheets()[0];

  var secondRowEnd = secondSheet.getLastRow();
  var submissionRowEnd = submissionSheet.getLastRow();

  var secondColumnEnd = secondSheet.getLastColumn();
  var submissionColumnEnd = submissionSheet.getLastColumn(); //used to define ranges dynamically

  var secondRange = secondSheet.getValues();
  var submissionRange = submissionSheet.getValues();

  for(var i = 2; i <= submissionRowEnd; i++) {

    var rock = secondRange.getCell(i, 1).getValue();
    var paper = secondRange.getCell(i, 2).getValue();
    var scissors = secondRange.getCell(i, 3).getValue();

    var status = secondRange.getCell(i, 4).getValue();
    var forStatus = secondRange.getCell(i, 5).getValue();

    if (status === "Do X!") {
      for(var j = 2; j <= submissionRowEnd; j++) {

        var jrock = submissionRange.getCell(j, 1).getValue();
        var jpaper = submissionRange.getCell(j, 2).getValue();
        var jscissors = submissionRange.getCell(j, 3).getValue();
        var jstatus = submissionRange.getCell(j, 4).getValue();

        if (status === forStatus) {
          jrock.setValue(rock);
          jpaper.setValue(paper);
          jscissors.setValue(scissors);
        } else { /*do nothing*/ }
      }
    }
  }

I've been staring at slightly different versions of this code for weeks now, so any and all eyes on this would be greatly, greatly appreciated!

like image 886
Maru Avatar asked Jul 19 '16 14:07

Maru


People also ask

What is getActiveRange?

getActiveRange()Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.

What is getRange in Google script?

getRange(row, col) : returns a reference to a single cell that is at the intersection of the row and column. The code below returns a reference to the range that is a single cell at the intersection of row 3 and column C. var range = SpreadsheetApp.getActiveSheet().getRange(3,3);

What does getRange return?

getRange method returns the range that is associated with the range change event.


2 Answers

Just checkout out the Apps Script API reference for the Spreadsheet Service. All the functions are documented there. Here are links to descriptions of the methods you need:

  • Spreadsheet.getActiveRange()
  • Sheet.getDataRange()

getRange() has multiple implementations.

  • Sheet.getRange(row, column)
  • Sheet.getRange(row, column, numRows)
  • Sheet.getRange(row, column, numRows, numColumns)
  • Sheet.getRange(a1notation)
like image 93
TheAddonDepot Avatar answered Sep 20 '22 19:09

TheAddonDepot


the usage of Sheet.getDataRange() yields to a fast response time compared to Sheet.getRange(), since Sheet.getDataRange() returns a data array

Hope This Helps (HTH)

like image 36
Trajano Roberto Avatar answered Sep 22 '22 19:09

Trajano Roberto