如何在HTML OnClick按钮中调用Google App脚本功能

时间:2017-12-28 13:58:28

标签: google-apps-script

我的Google表单包含在Open Show侧栏自定义菜单,在侧栏HTML文件中我添加了一些按钮,OnClick事件想要运行.gs文件中创建的功能

我尝试过使用删除(裁判支持门户)但是无法调用该功能的点击事件。

下面我附上了包含样本表的完整代码,请帮我解决问题

[样本表1



function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Custom Menu')
  
      .addItem('Show sidebar', 'showSidebar')
     
      .addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('Index')
      .setTitle('Sheet')
      .setWidth(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}



var Status_Set = "Deleted";

function Deleteuser() {
 var ss= SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName('Delete User');
  var startRow = 2 ;  // First row of data to process
  var numRows = sheet.getLastRow()-1;   
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)

 var data = dataRange.getValues();
 for(var i = 0; i < data.length; ++ i) {
  var lastName = data[i][0];
  var firstName = data[i][1];
  var email = data[i][2];
  
  };

   
AdminDirectory.Users.remove(email);


    Browser.msgBox('Your Request has been provisioned successfully', Browser.Buttons.OK_CANCEL);
Logger.log('Deleted');
  sheet.getRange(startRow + i,5).setValue(Status_Set);

  
 };
&#13;
<!DOCTYPE html>
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<style>
html,body,h1,h2,h3,h4,h5 {font-family: "Raleway", sans-serif}
</style>
<body class="w3-light-grey">

<!-- Top container -->
<div class="w3-bar w3-top w3-black w3-large" style="z-index:4">
  <button class="w3-bar-item w3-button w3-hide-large w3-hover-none w3-hover-text-light-grey"><i class="fa fa-dashboard"></i> My Dashboard</button>
</div>



<!-- !PAGE CONTENT! -->


  <br>
  <div class="w3-container w3-dark-grey w3-padding-32">
    <div class="w3-row">
      <div class="w3-container w3-third">
        <h5 class="w3-bottombar w3-border-green"><i class="fa fa-user" aria-hidden="true"></i> Manage users</h5>
        <button type="button"  onclick="google.script.run
          .Deleteuser()" >Delete</button>


  </div>
 </div>
 </div>

  <!-- End page content -->



</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要指定要运行的功能,并且可能包含成功和错误处理程序。这个文档解释了如何。 https://developers.google.com/apps-script/guides/html/reference/run

如何在主要删除用户类中添加“try”?您可以将错误返回给客户端或记录错误。

try {
  AdminDirectory.Users.remove(email);
} catch(e){
  Logger.log(e)
  return e
}

另外请注意,您的脚本“AdminDirectory.Users.remove(email)”将仅在具有G-suite管理员权限的帐户上运行,并且如果您已在脚本及其中打开目录API附加的API控制台。

通过将删除功能放在属于管理员的脚本中,然后向该脚本发送GET请求,可以解决管理权限问题。