title: How to update row data of google spreadsheet using google app script
body: <p>I am facing a problem on how to update the row of data in my database(google spreadsheet) using Google app script. Basically, I have a cell in the spreadsheet that has the value "Pending Approval" in it. When the script is executed, I want it to change the value in that cell to "Approved". I did lot of search but I can't figure out, how to do it correctly. I am newbie in this. Hope you can help me. Thanks!</p>
  
  <p><strong>Code.gs</strong></p>
  
  <pre><code> function doGet() {
    return HtmlService
        .createTemplateFromFile('Index')
        .evaluate();
  }
  
  function getData() {
    return SpreadsheetApp
        .openById('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE')
        .getActiveSheet()
        .getDataRange()
        .getValues();
  }
  
  function setApproved(ldap) 
  {
   // set the correct id and get the database
   var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE');
   var sss = SpreadsheetApp.openById(id).getActiveSheet();
  
   // Get all the data from the sheet
   var data = sss.getDataRange().getValues();
  
   // Get the headers and get the index of the ldap and the approval status
   // using the names you use in the headers
   var headers = data[0];
   var ldapIndex = headers.indexOf('ldap'); 
   var statusIndex = headers.indexOf('approvalstatus');
  
   // Declare the variable for the correct row number
   var sheetRow;
  
   // Iterate data, look for the correct row checking the ldap, start from 1 as 0==headers
   for( var i = 1 ; i &lt; data.length; i++ )
   {
     var row = data[i];
     if(row[ldapIndex] == ldap)
     { 
       // You have found the correct row, set + 1 because Sheet range starts from 1, not 0
       sheetRow = i +1;
       // We have found the row, no need to iterate further
       break;
     }
  }
   // Also set statusIndex +1, because index in array is -1 compared to index in sheet
   ++statusIndex;
   //Set the value
   sss.getRange(sheetRow, statusIndex ).setValue('Approved'); 
  }
  </code></pre>
  
  <p><strong>Index.html</strong></p>
  
  <pre><code> &lt;html&gt;
    &lt;head&gt;
      &lt;base target="_top"&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div  id="tables"&gt;
        &lt;? var data = getData();?&gt;
        &lt;table style="display: inline-block; border: 1px solid; float: left; " id="tableShift1"&gt;
        &lt;caption style=""&gt;Shift1&lt;/caption&gt;
            &lt;th style="border:2px solid black;"&gt;   Ldap   &lt;/th&gt;
            &lt;th style="border:2px solid black;"&gt;   Date   &lt;/th&gt;
            &lt;th style="border:2px solid black;"&gt;   Action   &lt;/th&gt;
          &lt;? for (var dataList = 1; dataList &lt; data.length; dataList++) {
               ?&gt;
            &lt;tr &gt;
              &lt;td style="border:1px solid black;"&gt;&lt;?= data[dataList][0] ?&gt;&lt;/td&gt;
              &lt;td style="border:1px solid black;"&gt;&lt;?= Utilities.formatDate(new Date(data[dataList][1]),"GMT+800", "yyyy-MM-dd"); ?&gt;&lt;/td&gt;
              &lt;td style="border:1px solid black;"&gt;&lt;button onclick='setApproved()' id='ldap'&gt;Approve&lt;/button&gt; &lt;button onclick='functionDecline()' id='button1'&gt;Decline&lt;/button&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;?   
            } ?&gt;
          &lt;/table&gt;
      &lt;/div&gt;
    &lt;/body&gt;
   &lt;script&gt;
   &lt;/script&gt;
  &lt;/html&gt;
  </code></pre>