简体   繁体   中英

Append a New Row on selected Tables in Google Docs with Apps Script

I have a Google Docs that has 4 tables, each table contains: Number, Date, Session, Name, Year of Class, and Department.

So whenever any new google form submitted, it will update and find the docs and the table based on department and date that the respondent choose. So the script already able to find the docs and fill the table based on the date, but the problem is it append the arrow on the next day table. Example: the date selected by the respondent is 05/02/2021, so it should also append the 05/02/2021 table right? instead of 06/02/2021.

Google Docs tables (before)

Google Docs tables (after a new response)

function appendTable(){
    if (pilihan1 == 'Advokasi dan Kesejahteraan Mahasiswa (ADVOKESMA)'){
      var range = bodyJadwalAdvo.findText(screening);
      var searchElement = bodyJadwalAdvo.findElement(DocumentApp.ElementType.TABLE, range);
    } else if (pilihan1 == 'Pengembangan Sumber Daya Mahasiswa (PSDM)'){
      var range = bodyJadwalPsdm.findText(screening);
      var searchElement = bodyJadwalPsdm.findElement(DocumentApp.ElementType.TABLE, range);
    } else if (pilihan1 == 'Informasi dan Komunikasi (INFOKOM)'){
      var range = bodyJadwalInfokom.findText(screening);
      var searchElement = bodyJadwalInfokom.findElement(DocumentApp.ElementType.TABLE, range);
    } else if (pilihan1 == 'Sosial Masyarakat (SOSMA)'){
      var range = bodyJadwalSosma.findText(screening);
      var searchElement = bodyJadwalSosma.findElement(DocumentApp.ElementType.TABLE, range);
    } else if (pilihan1 == 'Bisnis'){
      var range = bodyJadwalSosma.findText(screening);
      var searchElement = bodyJadwalSosma.findElement(DocumentApp.ElementType.TABLE, range);
    }
    element = searchElement.getElement();
    table = element.asTable();
    var tr = table.appendTableRow();
    
    function replaceBodyJadwal(depart){
      if(screening == '05/02/2021'){
        depart.replaceText('{{TanggalA}}', screening);
        depart.replaceText('{{NamaLengkapA}}', namaLengkap);
        depart.replaceText('{{AngkatanA}}', angkatan);
        depart.replaceText('{{Pilihan2A}}', pilihan2);
        tr.appendTableCell(" ");
        tr.appendTableCell("{{TanggalA}}");
        tr.appendTableCell(" ");
        tr.appendTableCell("{{NamaLengkapA}}");
        tr.appendTableCell("{{AngkatanA}}");
        tr.appendTableCell("{{Pilihan2A}}");
      } else if (screening == '06/02/2021'){
        depart.replaceText('{{TanggalB}}', screening);
        depart.replaceText('{{NamaLengkapB}}', namaLengkap);
        depart.replaceText('{{AngkatanB}}', angkatan);
        depart.replaceText('{{Pilihan2B}}', pilihan2);
        tr.appendTableCell(" ");
        tr.appendTableCell("{{TanggalB}}");
        tr.appendTableCell(" ");
        tr.appendTableCell("{{NamaLengkapB}}");
        tr.appendTableCell("{{AngkatanB}}");
        tr.appendTableCell("{{Pilihan2B}}");
      }  else if (screening == '07/02/2021'){
        depart.replaceText('{{TanggalC}}', screening);
        depart.replaceText('{{NamaLengkapC}}', namaLengkap);
        depart.replaceText('{{AngkatanC}}', angkatan);
        depart.replaceText('{{Pilihan2C}}', pilihan2);
        tr.appendTableCell(" ");
        tr.appendTableCell("{{TanggalC}}");
        tr.appendTableCell(" ");
        tr.appendTableCell("{{NamaLengkapC}}");
        tr.appendTableCell("{{AngkatanC}}");
        tr.appendTableCell("{{Pilihan2C}}");
      }  else if (screening == '08/02/2021'){
        depart.replaceText('{{TanggalD}}', screening);
        depart.replaceText('{{NamaLengkapD}}', namaLengkap);
        depart.replaceText('{{AngkatanD}}', angkatan);
        depart.replaceText('{{Pilihan2D}}', pilihan2);
        tr.appendTableCell(" ");
        tr.appendTableCell("{{TanggalD}}");
        tr.appendTableCell(" ");
        tr.appendTableCell("{{NamaLengkapD}}");
        tr.appendTableCell("{{AngkatanD}}");
        tr.appendTableCell("{{Pilihan2D}}");
      }
    }
    if (pilihan1 == 'Advokasi dan Kesejahteraan Mahasiswa (ADVOKESMA)'){
      replaceBodyJadwal(bodyJadwalAdvo); 
    } else if (pilihan1 == 'Pengembangan Sumber Daya Mahasiswa (PSDM)'){
      replaceBodyJadwal(bodyJadwalPsdm);
    } else if (pilihan1 == 'Informasi dan Komunikasi (INFOKOM)'){
      replaceBodyJadwal(bodyJadwalInfokom);
    } else if (pilihan1 == 'Sosial Masyarakat (SOSMA)'){
      replaceBodyJadwal(bodyJadwalSosma);
    } else if (pilihan1 == 'Bisnis'){
      replaceBodyJadwal(bodyJadwalBisnis);
    } 
  }

Solved.

function cariTabel(bodyJadwalDepartemen){
      if(screening=='05/02/2021'){
        screeningJadwal=null;
        var range = null;
      } else {
          if(screening=='06/02/2021'){
            screeningJadwal='05/02/2021';
          } else if(screening=='07/02/2021'){
            screeningJadwal='06/02/2021';
          } else if(screening=='08/02/2021'){
            screeningJadwal='07/02/2021';
          }
          var range = bodyJadwalDepartemen.findText(screeningJadwal);
      }     
      searchElement = bodyJadwalDepartemen.findElement(DocumentApp.ElementType.TABLE, range);
    }

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