title: How to allow access for importrange function via apps script?
body: <p>When you enter an importrange function manually into a spreadsheet you receive a pop up and must 'allow access'.<img src="https://i.stack.imgur.com/NF9VI.png" alt="Pop up to allow access for importrange"></p>
  
  <p>However, I'm trying to find a way to do this via a script because I'm creating many spreadsheets, each with a query-importrange function (I 'own' the spreadsheet which has data to import). There's too many for me to manually 'allow access' via the pop up and update the function to include the query function. </p>
  
  <p>Hence, I'm looking for a function call in apps script that can perform the same action that the pop up did. Code segment example below.</p>
  
  <p>Does anyone know of a function that can 'allow access'?
  Stefan</p>
  
  <pre><code>// create new spreadsheet file
  ...
  var ss = createSpreadsheet(fileName);
  var spreadsheet = SpreadsheetApp.open(ss);
  var sheet = spreadsheet.getSheetByName("Sheet1");
  
  // Add student as Viewer
  spreadsheet.addViewer(studentEmail);
  
  // Add ImportRange function 
  var sheet = spreadsheet.getSheets()[0];
  var cell = sheet.getRange("A1");
  var filter = "select * where Col3='" + studentEmail + "'";
  var qry = '=QUERY(importRange("' + fileKey + '","14-15S2!A1:AE");"' + filter + '";1)';
  cell.setValue(qry);
  // I need a function to 'allow access' here, so the function can be allowed access. Otherwise, it throws an error.
  ...
  </code></pre>