1
votes

I have a doGet(e) function in Google Script that writes to a spreadsheet, then returns a simple "success" message created with Content Service. My question is, is there a way to automatically close the tab/window created by this message? The user will be "approving" different items when they write to the spreadsheet, and it might get annoying to approve several items in a row using this function, then have to close each successive tab the function creates.

Here's the GS:

function doGet(e) {  
  var id = e.parameter.id;
  var fundnumber = e.parameter.fundnumber;
  var date = Utilities.formatDate(new Date(), "America/New_York", "MM/dd/yyyy");

  var sh = SpreadsheetApp.openById("12jWGJWHCLoiLVoA0TlBQ0QgOMxBU9gVj9HQQveiNg0w").getSheetByName("Purchase Order Meta");
  var data = sh.getDataRange().getValues();

  for(n=0;n<data.length;++n){
    if( data[n][1].toString().match(id)==id ){ 
      data[n][8] = 'Approved by Linda on ' + date;
      data[n][16] = 'Approved by Linda on ' + date + '. Awaiting order from Trish.';
      data[n][13] = fundnumber
    }; 
    sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
  }

  return ContentService.createTextOutput("success");
}

My understanding is that I have to return something either with Content Service or HTML Service in order to write a doGet. I've tried using HTML Service and add JS to the page to close the window, but it doesn't seem to work. :\

1

1 Answers

1
votes

Using the HTMLService output, you can close a popup or HTML page by wrapping google.script.host.close() in a timeout function, like so:

setTimeout( function() { google.script.host.close(); }, 3000);

Change your return to:

var output = "<body><p>Success!</p><script>setTimeout( function() { google.script.host.close(); }, 3000);</script></body>"

return HtmlService.createHtmlOutput(output);