Home > Back-end >  How to get lastRow from array of data from Google Sheet array?
How to get lastRow from array of data from Google Sheet array?

Time:10-16

I'm having no knowledge of JS or any leng, this code just fits me the best. So what I need to do, is to receive the last row from array which corresponds to today's date 13.10 today -> 13.10 today a row was added and send it to bot in Telegram, but the entire array is sending, like all rows from that array that contains 12.10.2021, 13.10.2021, etc.

This code should take the last added line (s) to the table depending on the date, if today's date is next to the line - this line is sent as a message in telegram.

But I have no idea how to force it to get last row(rows) from array. enter image description here

 const token = "my Token";

function timer() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let ws = ss.getSheetByName("SheetName");
  let data = ws.getRange(2, 1, ws.getLastRow()-1, 10).getValues();
  // console.log(data)
  let curentTime = new Date().getDate()   "."   (new Date().getMonth()   1)   "."   new Date().getFullYear();
  // console.log(curentTime)

  for (i = 0; i < data.length; i  ) {
    let dataInfo = data[i];
    // console.log(dataInfo)
    let clientName = dataInfo[3];
    // console.log(clientName)
    let clientBirthday  = dataInfo[0].toString();
    //  console.log(clientBirthday)
    let clientIdChat = "myID";
    if (dataInfo[0] !== ""){
      clientBirthday = dataInfo[0].getDate()   "."   (dataInfo[0].getMonth()   1)   "."   dataInfo[0].getFullYear();
      // console.log(clientBirthday)
    }
    
    if (clientBirthday == curentTime){
        sendText(clientIdChat,data.toString());
      

      
    }
  }
  

// console.log(curentTime)
}
 
function sendText(chatId, text, keyBoard) {
  let data = {
    method: 'post',
    payload: {
      method: 'sendMessage',
      chat_id: String(chatId),
      text: text,
      parse_mode: 'HTML',
      reply_markup: JSON.stringify(keyBoard)
    }
  }
  UrlFetchApp.fetch('https://api.telegram.org/bot'   token   '/', data);
}
> A/Дата надходження    B/Компанія    C/ІПН D/ПІБ E/Номер справи    F/Суд G/Область
 H/Місто відповідача   I/Адреса відповідача  J/Посилання на документ
> A/12.10.2021  B/A   C/1   D/Name    E/111/3433/99                 J/Link to document
> A/13.10.2021  B/B   C/2   D/name    E/111/3433/99                 J/Link to document

CodePudding user response:

I believe there are 2 issues:

  1. the sendText should be nested inside the for loop, so you are only executing that once you've matched the birthday
  2. the sendText formula should be sending dataInfo.toString not data.toString, since dataInfo reflects the current row while data is the entire array

I've re-written that segment of the code:

  for (i = 0; i < data.length; i  ) {
    let dataInfo = data[i];
    // console.log(dataInfo)
    
    let clientBirthday  = dataInfo[0].toString();
     console.log(clientBirthday)

    if (clientBirthday == curentTime){
          let clientName = dataInfo[3];
          //console.log(clientName)
          let clientIdChat = "myID";
          sendText(clientIdChat,dataInfo.toString());
    }
  }
  • Related