Pregunta

I am new to google apps scripts and to javascript so forgive my ignorance.

I have a google sheet that contains a list of asset tag numbers for computers. It also contains the username, full name, and serial number of the laptop that the student is using.

Example:

StudentName Username AssetTag SN

Student 1 username1 1002001 ####-##-####

Student 2 username2 1002002 ####-##-####

Student 3 username3 1002003 ####-##-####

Student 4 username4 1002004 ####-##-####

Student 5 username5 1002005 ####-##-####

Student 6 username6 1002006 ####-##-####

Student 7 username7 1002007 ####-##-####

Student 8 username8 1002008 ####-##-####

I need to be able to have the user input the asset tag number and have the script return the cell that it finds that number in, ideally to a variable.

The plan is to scan a barcode that inputs the asset tag and upon scanning have it fill in the rest of the information on a google form. So if someone scanned the barcode for 1002001 it would fill in the name, username, and SN fields for them. I'm hoping I can figure out the rest, but in order to do that I need the Cell that contains the asset number.

    function FindCell() {

  var ss = SpreadsheetApp.openById("0AmBYpL3aN9TZdDhZeGJnXzhRMkh2TkVTaHQtNnliSUE");
  var sheet = ss.getActiveSheet();
  var range = sheet.getDataRange();
  var values = range.getValues();
  var cell = findCell();
  Logger.log(cell);
}

Thank you!

P.S. I have been reading on here for answers to batch scripts forever, but now I'm trying my hand at google's brand of javascript. This is my first post...

¿Fue útil?

Solución

You're kind of on the right track potter but with a few problems. The first being that your function is named FindCell (with upper case F) and it calls a very similarly named function findCell (with lower case f) that isn't shown anywhere.

The script supplied doesn't show how you are getting the asset tags to work with. You probably have some function that gets it and declares the input as a variable. You need to pass that variable to the function that searches for a match. Your function above would not actually search for anything.

This is what I would do ( with commented statements to explain what's going on):

function getScannedInput() { /*I'm simulating the function you probably have which gets the scanned input
  You'll have your script to get the input here*/

  var scannedInput = 1002001 //I hardcoded the number you gave just for example

  var myInfo = FindCell(scannedInput); // You need to pass the value of the scanned input to the function
                                       //returns this 2D array [[StudentName, userName, SN, cellAddress]]
  var StudentName = myInfo[0][0];
  var userName = myInfo[0][1];
  var SN = myInfo[0][2];
  var cellAddress = myInfo[0][3]; //You may not need this now.
  /*Bear in mind that if you still need to use the cellAddress you'll need to
    set variables for the ss and sheet in this function too.*/

//  * Now do stuff with StudentName, userName & SN *
}

function FindCell(scannedInput) { // You need to pass the value of the scanned input to the function
  var ss = SpreadsheetApp.openById("1W3hkCk8foL7fQtmVkcU4ZP4eTbHT696cacR6a-sSeDY");
  var sheet = ss.getActiveSheet();
  var range = sheet.getDataRange();
  var values = range.getValues();
  var myInfo = []; //Create an empty array to put your info in when found
  var found = false;
  for (var i=0; i<values.length; ++i) { // Loops through each row
    for (var j=0; j<values[i].length; ++j) { // Loops through each column
      if (values[i][j] == scannedInput) { // Checks for match to scannedInput
        myInfo.push(values[i][0]);//Adds StudentName to the array
        myInfo.push(values[i][1]);//Add username to the array
        myInfo.push(values[i][3]);//Adds SN to the array
        myInfo.push(sheet.getRange(i, j).getA1Notation()); /*If you still need to return the cell address - 
        Do you still need to? (I think probably not.) Delete if you don't*/
        found = true;
        break; // Breaks out of the j loop
      }
    }
    if (found) {
      break;  // Breaks out of the i loop if found is true i.e. scannedInput was found
    }
  }
  return [myInfo]; /*returns this array [StudentName, userName, SN, cellAddress]
  or [StudentName, userName, SN] if you didn't need cellAddress above.*/
}

Editted after OP pointed out that myInfo returns 2D array. Here's a link to a sample sheet used in above code.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top