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

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

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

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

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

我正在使用 Google Apps 脚本 Oauth 并获得 facebook token ,这是成功生成 token 并访问 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 sheet 中工作,而不在网络应用程序或任何 html 浏览器或客户端中工作 那么如何将其引用到 code.gs

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

这是code.gs部分

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")}
}

最佳答案

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

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

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

关于javascript - 如何将 Google 表格脚本代码启动到 Google 应用程序脚本网络应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55056944/

相关文章:

javascript - 如何使用方括号保存 JSON 编码内容的数组?

JavaScript 正则表达式句柄/

facebook - 了解 Facebook SDK 应用请求与应用邀请

google-apps-script - Importhtml() 和 Importxml() "Could not fetch URL"- 仅在特定网页上

javascript - 如何根据主题标签更改事件链接的 CSS

javascript - 在您的网站上载自定义字体

php - Facebook PHP SDK - 图形返回错误 : Invalid OAuth access token

android - 如何在 android 中取消用户在 facebook 中喜欢的帖子

javascript - 压缩多个IF语句

google-apps-script - 以编程方式检索 Google Sheets 单元格边框样式