Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

How do I get the row number of a Google Sheets cell that contains a specific value in Google Apps Script?

I'm trying to create a roll marking system that reads from a Google Sheets spreadsheet, takes input via a Telegam bot, and fills in checkboxes on the Google Sheet. The flow is as follows:

  1. User picks a class (e.g. Year 7-8 Girls) via Telegram bot: https://imgur.com/v9PAgXR

  2. Look through Google Sheet for cells containing the class name (7-8 G): https://imgur.com/yuCckA1

  3. Extract the students' names from rows containing the class name, then presents them in a Telegram poll: https://imgur.com/q20I4iw

  4. Check the boxes corresponding to the student names.

Everything is going pretty well until step 4, where I can't seem to be able to get the actual row number that the student data came from, so I can't tell the script which checkbox to manipulate.

Here are some code snippets to give an idea of what I'm doing (I'm very new to all of this so open to suggestions/improvements):

  if (contents.message) { // If the bot receives a regular message
    var id_message = contents.message.from.id; 
    var firstName = contents.message.from.first_name;
    sendText(id_message, "Hi " + firstName +  "! Please choose a year group.", keyBoard)
  }

  else if (contents.callback_query) { // If the bot gets an inline keyboard input
    var names = [];
    var id_callback = contents.callback_query.from.id;
    var data = contents.callback_query.data;
    var filteredRows = rows.filter(function(row){
    if (row[4] == data) { // If the row contains the class code
      return row
    }
    });

    filteredRows.forEach(function(row) {
      names.push(row[0] + " " + row[1]) // Compile a list of the students in the class
    });

    if (filteredRows.length <= 10) { //
      sendPoll(id_callback,names)
    }

    else if (filteredRows.length >= 11) { //To get around the 10 option maximum for polls
      sendPoll(id_callback,chunkArray(names,9)[0])
    }
  }

And the code for sending the poll (completely hacky solution just as a proof of concept that I can manipulate the checkboxes). Someone please show me a better way of doing the string manipulation at the start, because it shouldn't take this many steps to go from "[1,2,3,4,5]" to "12345"!

  else if (contents.poll_answer) { // If the bot receives a poll response
  var pollOptions = JSON.stringify(contents.poll_answer.option_ids);
  var sliced = pollOptions.replace(/,/g,"");
  sliced = sliced.slice(1,-1);
  var i = sliced.length;

    if (pollOptions != '[]') {
      while (i--) {
        ss.getRange("H"+(parseInt(sliced.charAt(i),10)+7)).setValue("TRUE")
      }
    }

    else if (pollOptions == '[]') {
      ss.getRange("H7:H13" ).setValue("FALSE")        
    }
  }
question from:https://stackoverflow.com/questions/65950649/how-do-i-get-the-row-number-of-a-google-sheets-cell-that-contains-a-specific-val

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...