簡體   English   中英

為什么此Google Apps腳本的參數無法正常工作?

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

我在為以下代碼的第二個功能(此function findDate (text, word) )設置參數時遇到麻煩。 而不是使用此代碼:

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

我想使用其他:

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

因此,我可以在不同的上下文中調用此函數。 但是,當我向該函數添加兩個以上的參數時,例如該function findDate (text, word, position_word, position_date) ,它就不起作用了。 而且我不明白出了什么問題(日志消息似乎還可以)。 您能夠看到出什么問題了嗎? 我在這里缺少的JS函數屬性是什么?

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-我知道“位置日期”不是-1而是text2.length - 1 因此,“位置日期”實際上是text2.length - position_date而不是position_date。 我將此名稱僅用於記住該-1的含義。

在您的兩個參數版本中,您有代碼

date = text2[text2.length - 1];

在您的四個參數版本中,您有代碼

date = text2[text2.length - position_date];

但是您將-1作為新position_date參數的值傳遞給函數

我坐在這里等着你,直到一分錢掉下來8-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM