Jab Burgess
Jab Burgess

Reputation: 27

Google Apps Script to automatically open copied document after creating it?

We are using sheets for our mom & pop store as our invoice. However, my mother-in-law keeps saving over our invoice and we're always having to go back and delete the filled in sections. She cannot seem to remember the steps of making a copy and then opening that. I am using a script (button) to create a copy of the original and it is renamed as the customer name & date into a specific folder. However, once we do that, we still have to navigate to the folder and open the new document. Is there there a way to do this after I click the button on our original document to open of the copy that was made? Here is the script that I am using.

function saveAsSpreadsheet()
{ 
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var range = sheet.getRange('Carolina Fireworks Order Form!C8');
  sheet.setNamedRange('TestRange', range);
  var TestRange = sheet.getRangeByName('Carolina Fireworks Order Form!C8').getValues();
  Logger.log(TestRange); 
  var destFolder = DriveApp.getFolderById("1UdK90fEs3gkP4KZuUbmZbvvyVAW5ZMGw"); 
  let name = SpreadsheetApp.getActiveSheet().getRange('C8').getValue();
  const ds = Utilities.formatDate(new Date(),SpreadsheetApp.getActive().getSpreadsheetTimeZone(),"MM.dd.yy");
  DriveApp.getFileById(sheet.getId()).makeCopy('TestRange', destFolder).setName(`${name}.${ds}`); 
}

Upvotes: 1

Views: 462

Answers (1)

Tanaike
Tanaike

Reputation: 201553

In your situation, how about the following modification?

From:

DriveApp.getFileById(sheet.getId()).makeCopy('TestRange', destFolder).setName(`${name}.${ds}`); 

To:

const file = DriveApp.getFileById(sheet.getId()).makeCopy('TestRange', destFolder).setName(`${name}.${ds}`);
var js = `<script>window.open('${file.getUrl()}');google.script.host.close();</script>`;
var html = HtmlService.createHtmlOutput(js);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
  • In this script, the URL is opened using Javascript on the dialog.
  • This is from this post

Note:

  • From MetaMan's comment

    If you want this to work you will have to disable popup blocking

References:

Upvotes: 1

Related Questions