title: How do I execute a google script as 'owner' of a spreadsheet the user cannot view
body: <p>Hopefully this is quite a simple question!</p>
  
  <p>I've made a Google Script that writes to cells in a separate sheet "MasterSheet" (helped by several useful Q&amp;As from here). This will ultimately be deployed embedded to multiple different sheets that I'm giving to individual users.</p>
  
  <p>It works perfectly when the user has edit permissions on "MasterSheet", but I need that to remain private - i.e. not even viewable to anyone but me.</p>
  
  <p>As background: In each 'user sheet', IMPORTRANGE is used to import the columns from 'MasterSheet' that that user is allowed to view, and then the script allows the user to add a comment to the MasterSheet. 
  I can view MasterSheet to see all the columns with comments from various users on one unified sheet, but the individual users shouldn't be able to view this.</p>
  
  <p>The specific script for writing to the sheet is fairly generic:</p>
  
  <pre class="lang-js prettyprint-override"><code>function saveCommentToMasterSheet(form){
    var company = form.company,
        contact = form.contactselect,
        comment = form.message ,
        ss = SpreadsheetApp.openById("MASTERSHEET_ID").getSheetByName('MasterSheet');
    if(company=="all"){
      var row = findCell(contact);
    } else {
        var row = findCell2(contact,company);
    }
      //^The above finds the specific row number relating to the entry the user wants to comment on.
    var cell2 = ss.getRange(row,11);
      // ^In this case '11' is the column related to this sheet's specific user, I've made separate sheets for each user that are identical except this column number
    cell2.setValue(comment);
  }
  </code></pre>
  
  <p>I believe that I could make MasterSheet editable to anyone with the link, but I'd rather avoid that, particularly as the script is embedded in each spreadsheet so if the users just looked at it they'd find the MasterSheet id.</p>
  
  <p>I understand that it's possible to run a script as me using the execute API, but if I'm honest, I struggled a little to figure out make that work.</p>
  
  <p>Sorry if I'm asking a simple question - I've given it a good search and can't figure it out. </p>
  
  <p>Many thanks!
  Alex</p>
  
  <p>N.B. This <a href="https://stackoverflow.com/questions/16979901/running-a-google-script-from-within-a-spreadsheet-but-as-a-different-user">Running a google script from within a spreadsheet, but as a different user?</a> looks like a similar question, but I'd really like to keep the comment system within the user's spreadsheet.</p>