简体   繁体   English

如何从Google Sheet数组的数据数组中获取lastRow?

[英]How to get lastRow from array of data from Google Sheet array?

I'm having no knowledge of JS or any leng, this code just fits me the best.我对 JS 或任何长度一无所知,这段代码最适合我。 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.所以我需要做的是从对应于今天日期 13.10 今天的数组中接收最后一行 -> 今天 13.10 添加了一行并将其发送到 Telegram 中的机器人,但是整个数组正在发送,就像所有行一样包含 12.10.2021、13.10.2021 等的数组。

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.此代码应根据日期将最后添加的行(s)添加到表格中,如果今天的日期在该行的旁边 - 该行作为电报消息发送。

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

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 sendText应该嵌套在for循环中,因此您只有在匹配生日后才执行它
  2. the sendText formula should be sending dataInfo.toString not data.toString , since dataInfo reflects the current row while data is the entire array sendText公式应该发送dataInfo.toString而不是data.toString ,因为dataInfo反映当前行而data是整个数组

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());
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM