2

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?

Niek
  • 43
  • 2
  • 6
  • what services are you using ? are you using an oAuth authorization or just "normal" gas services like spreadsheetApp and MailApp ? – Serge insas Sep 25 '13 at 12:55
  • DocumentApp, MailApp, UserProperties, UiApp, DocsList and SpreadsheetApp. We are just using the normal gas services authorization. – Niek Sep 25 '13 at 13:25
  • Thanks, I didn't wait for your answer and suggested a solution below ;-) – Serge insas Sep 25 '13 at 13:41

1 Answers1

7

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>
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • What i mean is the Google Authorization popup. Like [link]https://developers.google.com/apps-script/images/new-auth-2.png – Niek Sep 25 '13 at 13:53
  • That's exactly what it does...it forces the Google pop up to appear by calling the authorize function, the UI is only there to guide the user in the process... – Serge insas Sep 25 '13 at 15:23
  • maybe I don't understand how this exactly work. The above code is in a Google Document right? What does the spreadsheet do within the authorize function? – Niek Sep 26 '13 at 05:16
  • Nothing really...you can remove it...It was there because the mail merge app takes its data from a spreadsheet. – Serge insas Sep 26 '13 at 05:23