title: Google Apps Script Error: "You do not have permission to call openById"
body: <p>I am trying to use a Google Script that I found on GitHub. (It is Posted Below.) It is supposed to merge data from Google sheets into a Google Doc. However, when I try to run the script I get an error: "You do not have permission to call openById". I have researched the problem and the information is conflicting. But, I have tried the following: </p>
  
  <ol>
  <li><a href="https://code.google.com/p/google-apps-script-issues/issues/detail?id=3832" rel="nofollow noreferrer">Manually run the function in script editor before adding the trigger to Google sheet.</a></li>
  <li>Authorized the script to access my Google Docs data.</li>
  <li><a href="https://stackoverflow.com/questions/22664183/google-script-error-you-do-not-have-permission-to-call-openbyid">Set the triggers for the function to "On Form Submit"</a></li>
  <li>Added the script to my Google Sheet and ran it from the menu as instructed: <em>Merge --> Fill Template</em></li>
  </ol>
  
  <p>I continue to get the error. I see that there are other people having the same problem, but the answers provided so far have not solved the issue. Any help would be greatly appreciated. </p>
  
  <pre><code>function doMerge() {
    var selectedTemplateId = "1foobarfoobarfoobarfoobarfoobarfoobar";//Copy and paste the ID of the template document here (you can find this in the document's URL)
  
    var templateFile = DriveApp.getFileById(selectedTemplateId);
    var mergedFile = templateFile.makeCopy();//make a copy of the template file to use for the merged File.
    mergedFile.setName("filled_"+templateFile.getName());//give a custom name to the new file (otherwise it is called "copy of ...")
    var mergedDoc = DocumentApp.openById(mergedFile.getId());
    var bodyElement = mergedDoc.getBody();//the body of the merged document, which is at this point the same as the template doc.
    var bodyCopy = bodyElement.copy();//make a copy of the body
  
    bodyElement.clear();//clear the body of the mergedDoc so that we can write the new data in it.
  </code></pre>