[英]How to save on the next row from one existing data of copied google spreadsheet to another google spreadsheet using google apps script?
[英]How to update row data of google spreadsheet using google app script
我在如何使用 Google 应用程序脚本更新我的数据库(谷歌电子表格)中的数据行时遇到问题。 基本上,我在电子表格中有一个单元格,其中包含“待批准”值。 执行脚本时,我希望它将该单元格中的值更改为“已批准”。 我做了很多搜索,但我不知道如何正确地进行搜索。 我是新手。 希望你能帮助我。 谢谢!
代码.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');
}
索引.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>
好的,因为您已将问题标记为 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');
}
我希望上面的东西对你有用,对我有用。 我希望它也不会太乱,我想出了一个简单的例子。 如果您有任何问题,请告诉我!
编辑:我在循环中添加了“中断”,因为我首先忘记了它,似乎它已经适得其反了。
编辑:我将添加一个图像作为示例。 该工作表演示了示例中使用的工作表类型以及我运行 setApproved('jaska') 后工作表的外观:
<td style="border:1px solid black;"><button onclick='google.script.run.setApproved("<?= data[dataList][0] ?>")' id='ldap'>Approve</button>
\n
您不需要使用 getId() 来获取 Id,因为您已经知道了。
var id = '17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE';
您的 onclick() 函数调用未传递任何参数。
<td style="border:1px solid black;"><button onclick='google.script.run.setApproved("jack")' id='ldap'>Approve</button> </td>
我找到了一个简单快捷的解决方案。 只需改变这个:
var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE');
对此:
var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE').getSheetByName('NAME_OF_UR_SHEET');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.