弹出授权码DocumentApp-Google Apps脚本 [英] Popup authorization code DocumentApp - Google Apps script

查看:121
本文介绍了弹出授权码DocumentApp-Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经创建了一个Google Apps脚本用于邮件合并.我们打开一个Google文档,然后在onOpen上运行一个脚本,以选择带有地址的电子表格,选择电子表格后,我们需要对代码进行授权.我们可以在脚本编辑器中进行授权,但是将针对每种邮件合并类型复制此文档,因此最终用户需要进行授权.好,这里就是问题发生的地方.最终用户将仅收到消息.

We have created a Google apps script for mail-merge purposes. We open a Google document and onOpen a script will run for selecting a spreadsheet with addresses and after selecting a spreadsheet we need to authorize the code. We can authorize within the script editor, but this document will be copied for every mail-merge type and therefor the end-user needs to authorize. Well here's where the problem occurs. The end-user will only get the message.

遇到错误:执行此操作需要授权."

"Error encountered: Authorization is required to perform that action."

可以,但是没有出现通常由网络应用显示的弹出窗口.因此,用户根本无法授权.我们不能要求最终用户转到脚本编辑器并运行代码,因此他们可以进行授权.

Okay fine, but the popup which normally shows by web-apps doesn't appear. So the user can't authorize at all. We can't ask our end-users to go to the script editor and run the code ones, so they will be able to authorize.

我们可以在脚本中手动弹出授权代码吗?

Can we popup the authorization code manually in the script?

推荐答案

这是我在仅使用常规GAS服务的文档嵌入式脚本中使用的例程,请尝试查看它是否符合您的要求.

Here is a routine I use in document embedded scripts that use only normal GAS services, try it to see if it meets your requirements.

function onOpen() {
  var ui = DocumentApp.getUi();
  if(!UserProperties.getProperty('author')){
    ui.createMenu('Custom Menu')
    .addItem("Authorize this app", 'authorize')
    .addToUi();
    var html = HtmlService.createHtmlOutputFromFile('index1')
    .setTitle("Install Menu").setWidth(400);
    ui.showSidebar(html);
  }else{
    ui.createMenu('Custom Menu')
    .addItem("Do something", 'doIt')
    .addToUi();
    var html = HtmlService.createHtmlOutputFromFile('index2')
    .setTitle("Mailmerge Menu").setWidth(400);
    ui.showSidebar(html);
  }
}


function authorize(){
  SpreadsheetApp.openById('0AnqSFd3iikE3dDRlSC05ZTNxb2xORzNnR3NmMllyeUE');
  UserProperties.setProperty('author','yes');
  var ui = DocumentApp.getUi();
  var html = HtmlService.createHtmlOutput('Authorization complete<br>Thanks<br><br>please refresh your browser').setTitle("Confirmation").setWidth(400);
  ui.showSidebar(html);
}

function doIt(){
  //
}

index1.html:

<div>
<style>
    body{
        font-family : verdana,arial,sans-serif;
        font-size : 10pt;
        background : beige;
        padding : 10px;
    }

    #content{
        margin-left:30px;
        margin-top:30px;
    }
</style>

<BODY LANG="fr-BE">

If you open this document for the first time please run the authorization process from the custom menu<br><br>
Thank you
<br>
</div>

index2.html:

<div>
<style>
    body{
        font-family : verdana,arial,sans-serif;
        font-size : 10pt;
        background : beige;
        padding : 10px;
    }

    #content{
        margin-left:30px;
        margin-top:30px;
    }
</style>

<BODY LANG="fr-BE">

Do what you have to do...

<br>
</div>

这篇关于弹出授权码DocumentApp-Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆