2
votes

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?

1
what services are you using ? are you using an oAuth authorization or just "normal" gas services like spreadsheetApp and MailApp ?Serge insas
DocumentApp, MailApp, UserProperties, UiApp, DocsList and SpreadsheetApp. We are just using the normal gas services authorization.Niek
Thanks, I didn't wait for your answer and suggested a solution below ;-)Serge insas

1 Answers

7
votes

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.

code.gs :

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>