如何使用谷歌应用脚​​本

时间:2018-02-05 06:56:44

标签: google-apps-script

我正面临着如何使用Google应用脚本更新数据库(Google电子表格)中的数据行的问题。基本上,我在电子表格中有一个单元格,其值为"待批准"在里面。当脚本执行时,我希望它将该单元格中的值更改为"已批准"。我做了很多搜索,但我无法弄清楚,如何正确地做到这一点。我是新手。希望您能够帮助我。谢谢!

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'); 
}

的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>

4 个答案:

答案 0 :(得分:1)

好的,因为您已将问题标记为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'); 
}

我希望上面的内容适合你,为我做的。 我希望它也不会太乱,我想出了一个简单的例子。 如果您有任何疑问,请告诉我!

编辑:我在循环中添加了'break',因为我先忘了它,看起来它已经适合你了。

编辑:我将添加一个图像作为示例。该工作表演示了示例中使用了哪种工作表以及在我运行setApproved('jaska')之后工作表的样子: example

答案 1 :(得分:1)

<td style="border:1px solid black;"><button onclick='google.script.run.setApproved("<?= data[dataList][0] ?>")' id='ldap'>Approve</button>

答案 2 :(得分:0)

您不需要使用getId()来获取ID,因为您已经知道。

var id =&#39; 17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE&#39 ;;

  • 在您的Index.html上,我发现您没有使用google.script.run来调用服务器的功能 google.script.run.setApproved([参数])
  • 您的onclick()函数调用未传递任何参数。

    <td style="border:1px solid black;"><button onclick='google.script.run.setApproved("jack")' id='ldap'>Approve</button> </td>

答案 3 :(得分:0)

我找到了一个简单快捷的解决方案。只需更改此:

var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE');

为此:

var id= SpreadsheetApp.getActiveSpreadsheet().getId('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE').getSheetByName('NAME_OF_UR_SHEET');