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

问题描述 投票:0回答:1

我有一个Google工作表代码,其中包含一个UI部分,即html和code.gs问题在Google工作表中我正在获取一个网页,并且身份验证和其他功能运行良好但是如果我在code.gs中添加了doGet()并且将其转换为webapp函数不起作用,并且scriptlet也在html页面中显示,函数也已破坏。

由于html页面中的代码在google应用程序脚本表的服务器端运行,但它无法在客户端运行,因为引用未指向服务器端code.gs

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

我正在使用Google Apps Scripts 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工作表中工作,但不能在web应用程序或任何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")}
}
javascript facebook-graph-api google-apps-script google-sheets
1个回答
0
投票

终于找到了解决方案,需要在html文件和gs文件中添加html标签和body标签我们应该添加

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

它应该返回一个没有括号的html页面

© www.soinside.com 2019 - 2024. All rights reserved.