[英]How to update row data of google spreadsheet using google app script
I am facing a problem on how to update the row of data in my database(google spreadsheet) using Google app script.我在如何使用 Google 应用程序脚本更新我的数据库(谷歌电子表格)中的数据行时遇到问题。 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代码.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');
}
Index.html索引.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>
Ok, as you have tagged the question as Google-App-Script, I presume you know how to call the function from the client side.好的,因为您已将问题标记为 Google-App-Script,所以我假设您知道如何从客户端调用该函数。
So as the ldap does not occur in the database twice, you could use it as an id with a function like following:因此,由于 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');
}
I hope something like above works for you, did for me.我希望上面的东西对你有用,对我有用。 I hope it is not too messy either, a quick example I came up with.我希望它也不会太乱,我想出了一个简单的例子。 Do let me know if you have any questions !如果您有任何问题,请告诉我!
EDIT: I added the 'break' to the loop, as I forgot it first and seems it already backfired for you.编辑:我在循环中添加了“中断”,因为我首先忘记了它,似乎它已经适得其反了。
EDIT: I will add an image for an example.编辑:我将添加一个图像作为示例。 The sheet demonstrates what kind of sheet is used for the example and what the sheet looks like after I have once run setApproved('jaska') :该工作表演示了示例中使用的工作表类型以及我运行 setApproved('jaska') 后工作表的外观:
<td style="border:1px solid black;"><button onclick='google.script.run.setApproved("<?= data[dataList][0] ?>")' id='ldap'>Approve</button>
\n
You don't need to use getId() to get the Id because you already know.您不需要使用 getId() 来获取 Id,因为您已经知道了。
var id = '17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE'; var id = '17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE';
Your onclick() function call did not pass any parameter.您的 onclick() 函数调用未传递任何参数。
<td style="border:1px solid black;"><button onclick='google.script.run.setApproved("jack")' id='ldap'>Approve</button> </td>
I found a easy and quick solution.我找到了一个简单快捷的解决方案。 Just change this:只需改变这个:
var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE');
To this:对此:
var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE').getSheetByName('NAME_OF_UR_SHEET');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.