如何将Google表格脚本代码启动到Google应用程序脚本网络应用程序中?

时间:2019-03-08 04:55:33

标签: javascript facebook-graph-api google-apps-script google-sheets

我有一个Google表格代码,其中包含一个UI部分,即html和code.gs 问题出在Google工作表内,我正在获取一个网页,并且身份验证和其他功能运行良好,但是如果我在code.gs中添加doGet()并将其转换为webapp,则该功能将无法正常工作,并且Scriptlet将显示在html中页面上的功能也坏了。

由于html页面中的代码正在google app脚本表的服务器端运行,但是由于引用未指向服务器端代码,因此无法在客户端运行。gs

我需要使其与客户端代码一起使用,它应该像在Google电子表格中一样起作用

我正在使用Google Apps脚本Oauth 并获取一个Facebook令牌,这是成功生成令牌并访问Facebook数据Original Work

的原始工作

这些是代码

     
     
     
     
     <div class="tab-content">
        
        <!-- Home -->
        <div id="ads" class="tab-pane fade in active">
        <div id="page-content-wrapper">
            <div class="hamburger is-closed" data-toggle="offcanvas">
                <span class="hamb-top"></span>
    			<span class="hamb-middle"></span>
				<span class="hamb-bottom"></span>
            </div>
            <div class="container">
            
            
           <? if(!getService().hasAccess()) { ?> 
                <div class="row">
                    <div class="col-md-12 col-lg-12">
                    <p style="font-size:25px;">Features:</p>
                   
                    </div>
                </div>
                <br />
     <div class="row">
    <div class="col-md-12" align='center'>
    <a class="loginBtn loginBtn--facebook btn btn-info" href='<?=getService().getAuthorizationUrl()?>' target='_blank'>Authorize Facebook</a>
    </div>
    </div>
    
    <? } else { ?>
    
    <p style='color:green'>Authorization Success</p>
    
<div class="form-group">
<label for="accountData">Select Account:</label><span class="glyphicon glyphicon-question-sign help"  title="Select Account To Export"></span>
<select id="accountData" style="width: 100%">
 <? if (getService().hasAccess()) { ?>
 <? var data = adAccounts() ?>
 <? if (data != false) { ?>
<? data = data['facebookAccountData'] ?>
<optgroup label="Ad Accounts">
<? for (i=0; i<data.length; i++) { ?>
    <option value="<?= data[i]['account_id'] ?>" data-type="facebookAds"><?= data[i]['name'] ?> (<?= data[i]['account_id'] ?>)</option>
   <? } ?>
</optgroup>
 
 <? } ?>
 
   <? } ?>
   
   </select>
</div>    

登录按钮本身在code.gs内部调用一个函数,但仅在Google工作表内起作用,而不能在网络应用程序或任何html浏览器或客户端中起作用 那么如何将此引用到code.gs

 href='<?=getService().getAuthorizationUrl()

这是code.gs PART

function fbAuth(){
  var UI=HtmlService.createTemplate("<b><a href='<?=getService().getAuthorizationUrl()?>' target='_blank'>Click To Authorize</a></b><br /><? if(getService().hasAccess())"+ 
                                    "{ ?> <?!= <p><span style='color:green'>Authorized Successfully</span></p> } else {?> <?!= <p><span style='color:red'>Not Authorized</span></p> }").evaluate() 
  SpreadsheetApp.getUi().showModalDialog(UI, "Facebook Authorization")
 
}







function getService() {
  return OAuth2.createService('Facebook')
      // Set the endpoint URLs.
      .setAuthorizationBaseUrl('https://www.facebook.com/dialog/oauth')
      .setTokenUrl('https://graph.facebook.com/v3.2/oauth/access_token')

      // Set the client ID and secret.
      .setClientId(CLIENT_ID)
      .setClientSecret(CLIENT_SECRET)

      // Set the name of the callback function that should be invoked to complete
      // the OAuth flow.
      .setCallbackFunction('authCallback')
  
  
      //Set Scope
      .setScope('ads_read manage_pages publish_pages pages_show_list')
      
  

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getUserProperties());
}


function authCallback(request) {
  var isAuthorized = getService().handleCallback(request);
 
  if (isAuthorized) {
    successUI(true)
    showBar()
    return HtmlService.createHtmlOutput('Success! You can close this tab.<script>window.top.close()</script>');
  } else {
    successUI(false)
    showBar()
    return HtmlService.createHtmlOutput('Denied. You can close this tab.<script>window.top.close()</script>');
  }
}


function reset() {
  var service = getService();
  service.reset();
  showBar()
  SpreadsheetApp.getUi().alert("Token Reset Success!!")
}

function successUI(isAuth){

 if(isAuth){
  var UI=HtmlService.createTemplate("<b><span style='color:green'>Authorization Successful</span></b>").evaluate()
  SpreadsheetApp.getUi().showModalDialog(UI, "Authorization Status") } else
  {var UI=HtmlService.createTemplate("<b><span style='color:red'>Authorization Fail</span></b>").evaluate()
    SpreadsheetApp.getUi().showModalDialog(UI, "Authorization Status")}
}

1 个答案:

答案 0 :(得分:0)

最后找到分辨率,需要在html文件中添加html标签和body标签 在gs文件中,我们应该添加

function doGet(){
 return HtmlService.createTemplateFromFile('html').evaluate()
  
}

它应该返回不带括号的html页面