skip to Main Content

hy guys im learning google apps script to telegram bot,
i have write auto reply command and it success to reply, if i sent @02-02-2021 it will show all recorded data on that day, but on my code it just return only 1 row, i have trying anything but nothing work.
thanks for helping,

function searchDataByTanggalTransaksi(Tanggal){
  var rangeNameTransaksi = "Transaksi!A2:O";
  var rowsTransaksi = Sheets.Spreadsheets.Values.get(MYSSID, rangeNameTransaksi).values;
  var panjangTransaksi = rowsTransaksi.length;
  var Tanggal, Nama_Item, Divisi, Sat, Qty, Harga_Satuan, Debet, Kredit, Saldo, Peruntukan, Total  = "";
  var pesan ="";
    for (var row = 0; row < panjangTransaksi; row++ ){
    if ("@" + rowsTransaksi[row][1]==Tanggal){
      Tanggal = "📅   Tanggal Transaksi : " + rowsTransaksi[row][1];
      Nama_Item = rowsTransaksi[row][2];
      Divisi = rowsTransaksi[row][3];
      Sat = rowsTransaksi[row][4];
      Qty = rowsTransaksi[row][5];
      Harga_Satuan = rowsTransaksi[row][6];
      Debet = rowsTransaksi[row][7];
      Kredit = rowsTransaksi[row][8];
      Saldo = rowsTransaksi[row][9];
      Peruntukan = rowsTransaksi[row][12];
      Total = "💸   Total Transaksi : " + rowsTransaksi[row][14] + ",-";
      pesan += "<code>- " + Nama_Item + " |" + Qty + " " + Sat + " |" + Kredit + "</code>";
      
      return pesan ;
    }
   
  }
  return "Data tanggal tidak ditemukan";
}
function testgetRowsTransaksi(){
  var tanggal = searchDataByTanggalTransaksi("@2");
  var x = ""
  }

4

Answers


  1. Chosen as BEST ANSWER

    i have found the work around, on above method i was use return, but when i sendText it reply all the rows

    case "/tgl" :
          var ssIdLogistik ="mySsId";
          var rangeNameTransaksi = "Transaksi!A2:O";
          var rowsTransaksi = Sheets.Spreadsheets.Values.get(ssIdLogistik, rangeNameTransaksi).values;
          var panjangTransaksi = rowsTransaksi.length;
          var Tanggal, Nama_Item, Divisi, Sat, Qty, Harga_Satuan, Debet, Kredit, Saldo, Peruntukan, Total  = "";
          for ( var row = 0; row < panjangTransaksi; row++){
            if (rowsTransaksi[row][1] == perintah[1]){
              Tanggal = "📅   Tanggal Transaksi : " + rowsTransaksi[row][1];
              Nama_Item = rowsTransaksi[row][2];
              Divisi = rowsTransaksi[row][3];
              Sat = rowsTransaksi[row][4];
              Qty = rowsTransaksi[row][5];
              Harga_Satuan = rowsTransaksi[row][6];
              Debet = rowsTransaksi[row][7];
              Kredit = rowsTransaksi[row][8];
              Saldo = rowsTransaksi[row][9];
              Peruntukan = rowsTransaksi[row][12];
              Total = "💸   Total Transaksi : " + rowsTransaksi[row][14] + ",-";
              
              text += "<code>- " + Nama_Item + " |" + Qty + " " + Sat + " |" + Kredit+ ",-" + "</code>n";
            }        
          }
          if(text == ""){
              text = "🚫  Periksa format penulisan kode permintaan Tanggal.nn" +
                     "Melihat transaksi berdasarkan tanggal di awali dengan '/tgl_' dengan format /tgl_hh-bb-tttt:n" +
                     "    👉🏻   /tgl_02-02-2021 n";}      
          sendText(updates.message.chat.id, logoData + "<code>" + Tanggal + "n----------n" + text + "----------n" + Total + "</code>");
          break;
    

  2. One approach is to store found values in array, I assume that you want to return pesan right?

    So solution one of possible solutions is that every record that is found you store it into array, and then return whole array(if nothing found array length will be 0).

    Here is there modified code:

    function searchDataByTanggalTransaksi(Tanggal) {
      var rangeNameTransaksi = "Transaksi!A2:O";
      var rowsTransaksi = Sheets.Spreadsheets.Values.get(MYSSID, rangeNameTransaksi)
        .values;
      var panjangTransaksi = rowsTransaksi.length;
      var Tanggal,
        Nama_Item,
        Divisi,
        Sat,
        Qty,
        Harga_Satuan,
        Debet,
        Kredit,
        Saldo,
        Peruntukan,
        Total = "";
      var pesan = "";
    
      var resultArray = []; //Mentioned array to pass data in.
    
      for (var row = 0; row < panjangTransaksi; row++) {
        if ("@" + rowsTransaksi[row][1] == Tanggal) {
          Tanggal = "📅   Tanggal Transaksi : " + rowsTransaksi[row][1];
          Nama_Item = rowsTransaksi[row][2];
          Divisi = rowsTransaksi[row][3];
          Sat = rowsTransaksi[row][4];
          Qty = rowsTransaksi[row][5];
          Harga_Satuan = rowsTransaksi[row][6];
          Debet = rowsTransaksi[row][7];
          Kredit = rowsTransaksi[row][8];
          Saldo = rowsTransaksi[row][9];
          Peruntukan = rowsTransaksi[row][12];
          Total = "💸   Total Transaksi : " + rowsTransaksi[row][14] + ",-";
          pesan +=
            "<code>- " +
            Nama_Item +
            " |" +
            Qty +
            " " +
            Sat +
            " |" +
            Kredit +
            "</code>";
    
          return resultArray.push(pesan); //Data passed in per every iteration.
        }
      }
      return resultArray; //returning line
      return "Data tanggal tidak ditemukan"; // this line is obsolete
    }
    function testgetRowsTransaksi() {
      var tanggal = searchDataByTanggalTransaksi("@2");
      var x = "";
    }
    
    
    Login or Signup to reply.
  3. Though I’m not pretty sure, what kind of data panjangTransaksi variable holds, if you just want the all matching items in a concatenated string, you could try like below

        function searchDataByTanggalTransaksi(Tanggal){
            var rangeNameTransaksi = "Transaksi!A2:O";
            var rowsTransaksi = Sheets.Spreadsheets.Values.get(MYSSID, rangeNameTransaksi).values;
            var panjangTransaksi = rowsTransaksi.length;
            return panjangTransaksi.reduce((acc, ele) => {
                let Nama_Item, Divisi, Sat, Qty, Harga_Satuan, Debet, Kredit, Saldo, Peruntukan, Total  = "";
                if ("@" + ele[1] == Tanggal){
                    Tanggal = "📅   Tanggal Transaksi : " + ele[1];
                    Nama_Item = ele[2];
                    Divisi = ele[3];
                    Sat = ele[4];
                    Qty = ele[5];
                    Harga_Satuan = ele[6];
                    Debet = ele[7];
                    Kredit = ele[8];
                    Saldo = ele[9];
                    Peruntukan = ele[12];
                    Total = "💸   Total Transaksi : " + ele[14] + ",-";
                    acc += "<code>- " + Nama_Item + " |" + Qty + " " + Sat + " |" + Kredit + "</code>";
                  }
                  return acc;
            }, '') ||  "Data tanggal tidak ditemukan";
          }
    Login or Signup to reply.
  4. The program will never enter this if ("@" + rowsTransaksi[row][1]==Tanggal) as Tanggal is undefined.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search