简体   繁体   中英

How to pull a different value from google sheets each time it runs

So I am trying to pull the value that is in the latest line of google sheets . Here is the part of the script that I am having trouble with. This script is being written within google scripts.

When this script runs the confirmation email it sends has the value of var cell as undefined . 0

function formSubmitReply(e) {
  var userEmail = e.values[3];
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  // Set the status of the new ticket to 'New'.
  // Column F is the Status column
  sheet.getRange(lastRow, getColIndexByName("Status")).setValue("New");
  var supportEmail;

  // Calculate how many other 'New' tickets are ahead of this one
  var numNew = 0;
  for (var i = 2; i < lastRow; i++) {
    if (sheet.getRange(i, getColIndexByName("Status")).getValue() == "New") {
      numNew++;
    }
  }

  var range = sheet.getRange("B2:B100");

  var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
  Logger.log(values[0][0]);
  for(n=0;n<values[0].length;++n){
    var cell = values[n][1]
  }

The problem is here:

for(n=0;n<values[0].length;++n){
    var cell = values[n][1]
  }

values[0].length is the number of columns while values[n][1] goes through rows.

if you need a loop to go over the rows use this:

for(n=0;n<values.length;++n){
        var cell = values[n][1]
      }

Otherwise if you have 10 columns and 50 rows, your loop will only go up to ten as values[0].length is 10 and you will pull data from:

  1. A1
  2. A2
  3. A3
  4. A4

and etc until

  1. A10

and then loop will stop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM