0

I facing the problem on how i ganna code this. I tried to do google.script.run but not working. When i click the button Approved, the button will throw data to the function. I dont know what to do next. I have no more idea how it works. Thanks!

Index.html

<body>
    <div  id="tables">
      <? var data = getData();?>
      <table id="tableShift1">
      <caption >Shift1</caption>
          <th>   ID   </th>
          <th>   Ldap   </th>
          <th>   Action   </th>
        <? for (var dataList = 1; dataList < data.length; dataList++) {
             ?>
          <tr >
            <td><?= data[dataList][0] ?></td>
            <td><?= data[dataList][1] ?></td>
            <td><button onclick='google.script.run.setApproved(data[dataList][0])' id='btnLdap'>Approve</button></td>
          </tr>
        <?   
          } ?>
        </table>
    </div>
  </body>

Code.gs

function setApproved(data) {
  var ss = SpreadsheetApp.openById('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE');
  var sheet = ss.getActiveSheet();
  var dataRange = sheet.getDataRange();
  var values = dataRange.getValues();

 var headers = values[0];
 var ldapIndex = headers.indexOf('ID'); 
 var statusIndex = headers.indexOf('approvalstatus');

 var sheetRow;

 for( var i = 1 ; i < values.length; i++ )
 {
   var row = values[i];
   if(row[ldapIndex] == data)
   { 
     sheetRow = i +1;
     break;
   }
}
 ++statusIndex;
 sheet.getRange(sheetRow, statusIndex).setValue('Approved');
}
4
  • Explain clearly what it is you're trying to do, and what actually happens when you run the above code. Commented Feb 7, 2018 at 13:29
  • I cant get the data from the table when i click the button Approve. <td><button onclick='google.script.run.setApproved(data[dataList][0])' id='btnLdap'>Approve</button> this is the code in html im facing now. i dont know how i ganna code to get the data i want. Sorry in my english. Commented Feb 7, 2018 at 13:39
  • In should be this, 'google.script.run.setApproved("<?= data[dataList][0] ?>")'. Try that Commented Feb 7, 2018 at 13:51
  • It works! Thanks @Jack Brown Commented Feb 7, 2018 at 13:53

1 Answer 1

1

The issue was in the following

<td><button onclick='google.script.run.setApproved(data[dataList][0])' id='btnLdap'>Approve</button>

Since, the value of data[dataList][0] needs to be send. We need to use printing scriptlets to define the exact value at the time of html evaluation. It should be:

<td><button onclick='google.script.run.setApproved("<?= data[dataList][0] ?>")' id='btnLdap'>Approve</button>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.