简体   繁体   中英

Why the parameters of this Google Apps Script are not working as expected?

I'm having trouble setting the parameters for the second function (this one function findDate (text, word) ) of the code below. Rather than use this code:

if (text2[text2.length - 2] === word) {
   date = text2[text2.length - 1];

I would like to use this other:

if (text2[text2.length - position_word] === word){
   date = text2[text2.length - position_date];

So I could invoke this function in different contexts. However, when I add two more parameters to this function, like this function findDate (text, word, position_word, position_date) , it just doesn't work. And I can not understand what goes wrong (log messages seem to be OK). You are able to see what is wrong? What is the JS function property that I'm missing here?

function setColumnIfromG() {
  var s = SpreadsheetApp.getActiveSheet();
  var input = s.getRange(2, 7, s.getLastRow(), 1).getValues();
  var output = [];
  for (var i = 0; i < input.length; i++) {
      output.push([ findDate(input[i][0], 'common term') ]);
   // output.push([ findDate(input[i][0], 'common term', -2, -1) ]);
      s.getRange(2, 9, output.length, 1).setValues(output);
    }
 }

function findDate (text, word){
//function findDate (text, word, position_word, position_date){
  Logger.log('text = '+ text);
  var text1 = text.split(".Date");
  Logger.log("text1 = "+ text1);
  var date = 'no date informed';
  for (var i=0; i<text1.length; i++) {
    var text2 = text1[i].split(" ");
    Logger.log("text2 = " + text2);
    Logger.log("text2[text2.length - 2] = " + text2[text2.length - 2]);
    Logger.log("text2[text2.length - 1] = " + text2[text2.length - 1]);

//    Logger.log("text2.length = " + text2[text2.length]);
//    Logger.log("position_word = " + position_word);
//    Logger.log("position_date = " + position_date);

    if (text2[text2.length - 2] === word){
      date = text2[text2.length - 1];
//    if (text2[text2.length - position_word] === word) {
//        date = text2[text2.length - position_date];
    }
      else {
      date = 'no date informed';
      }
   }
   return date;
}

PS - I know that the "position date" is not -1 but text2.length - 1 . So, "position date" is really text2.length - position_date and not position_date. I put this name only to remember-me of what this -1 is related to.

In your two param version you have the code

date = text2[text2.length - 1];

In your four param version you have the code

date = text2[text2.length - position_date];

But you pass -1 in to the function as the value of the new position_date param

I'll sit and wait here looking at you until the penny drops 8-)

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