MOO*_*MOO 3 google-apps-script
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!
Code.gs
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 < 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');
}
Run Code Online (Sandbox Code Playgroud)
Index.html
<html>
<head>
<base target="_top">
</head>
<body>
<div id="tables">
<? var data = getData();?>
<table style="display: inline-block; border: 1px solid; float: left; " id="tableShift1">
<caption style="">Shift1</caption>
<th style="border:2px solid black;"> Ldap </th>
<th style="border:2px solid black;"> Date </th>
<th style="border:2px solid black;"> Action </th>
<? for (var dataList = 1; dataList < data.length; dataList++) {
?>
<tr >
<td style="border:1px solid black;"><?= data[dataList][0] ?></td>
<td style="border:1px solid black;"><?= Utilities.formatDate(new Date(data[dataList][1]),"GMT+800", "yyyy-MM-dd"); ?></td>
<td style="border:1px solid black;"><button onclick='setApproved()' id='ldap'>Approve</button> <button onclick='functionDecline()' id='button1'>Decline</button></td>
</tr>
<?
} ?>
</table>
</div>
</body>
<script>
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
好的,因为您已将问题标记为 Google-App-Script,所以我假设您知道如何从客户端调用该函数。
因此,由于 ldap 不会在数据库中出现两次,您可以将其用作具有如下功能的 id:
function setApproved(ldap)
{
// set the correct id and get the database
var id= SpreadsheetApp.getActiveSpreadsheet().getId();
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 < 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');
}
Run Code Online (Sandbox Code Playgroud)
我希望上面的东西对你有用,对我有用。我希望它也不会太乱,我想出了一个简单的例子。如果您有任何问题,请告诉我!
编辑:我在循环中添加了“中断”,因为我首先忘记了它,似乎它已经对你产生了适得其反的效果。
编辑:我将添加一个图像作为示例。该工作表演示了示例中使用的工作表类型以及我运行 setApproved('jaska') 后工作表的外观:
归档时间: |
|
查看次数: |
14633 次 |
最近记录: |