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!
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.
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);
getRange method returns the range that is associated with the range change event.
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:
getRange() has multiple implementations.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With