0

I'm trying to change this script to search for the date (now) and color the found date in the sheet.

Original script;

function findThis(val) {
var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var searchStr = "Foo";
Logger.log(res);
var res = findValue(data, searchStr);
if (res != null) {
Browser.msgBox("Found in row " + res[0] + ", column " + res[1]);
}
}

function findValue(data, obj) {
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
  if (data[i][j] == obj) {
    return [i, j];
  }
}
}
return null;

}

My trial and error script;

I think the range is in res but when i want to getRange it does not accept the res coordinates, I've tryed getA1Notation() I am doing it wrong, but stil learning and trying, maybe you can help.

    function findThis(val) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // toevoeging
  var data = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  var Today = new Date();
  var date = Utilities.formatDate(Today,"GMT+100","dd-M-yyyy");
  Logger.log(date)
  var searchStr = date;

  var res = findValue (data, searchStr);
  if (res != null) {
  Logger.log(res);
    Logger.log(res[0]+", "+res[1]);
    var Range = (res[0]+", "+res[1]);
    var x = (res[0]+", "+res[1]);
    //var C = range.getColumnIndex(res);
    var range = Range.getA1Notation()
    Logger.log(range); 
    var value = sheet.getRange([x]).getValue(); //toegevoegd
    Logger.log(value);
    Logger.log(x);    
    sheet.getRange(x).setBackgroundColor('Yellow'); 
    Browser.msgBox("Found in row " + res[0] + ", column " + res[1]);
  }
}

function findValue(data, obj) {
  for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].length; j++) {
      if (data[i][j] == obj) {
        return [(i+1), (j+1)];
      }
    }
  }
4

1 回答 1

0

It looks like you are making some simple mistakes in understanding javascript objects and their methods. (Google apps-script, GAS, is a derivative of javascript, so any resource that will help you learn basic javascript will improve your skills here.)

A Range is an object of Class Range, while the a1Notation expression of a range in a Spreadsheet (e.g. "A1", or "A:Z") is an object of Class String. Now, a String has attributes and methods that are useful for doing the things you might want to do with a string of text. A Range object, on the other hand, has different attributes and methods that are appropriate to it - they are documented here. For them to work on a variable object in your script, that variable must be of Class Range. Let's look at this line in your code:

var Range = (res[0]+", "+res[1]);

That line creates a variable named Range, which is a String containing something like "15, 16". Then, when you try Range.getA1Notation() it fails because strings don't have that method. What you need to do is use an actual Range object. Here's one way that will work for you, just replace the corresponding block in your current script:

if (res !== null) {
  var range = sheet.getRange(res[0],res[1]);
  range.setBackgroundColor('Yellow'); 
  Browser.msgBox("Found at " + range.getA1Notation());
}

You don't need the variable value for anything in your example, but if you did, this is how you would obtain it.

  var value = range.getValue(); //toegevoegd
  Logger.log(value);

Like @Sergeinsas, I've got some concerns about the way you're comparing dates. But if it's working for you, that's great.

于 2013-04-12T18:38:56.503 回答