user14807564
user14807564

Reputation:

Open Existing Spreadsheet via Google App Script

Trying to open a existing Google Spreadsheet with the Google App Script

and the script works fine but it does not open the Spreadsheet.

can someone please check that what is the problem. Your help will be greatly appreciated.

Or is there any way to open the existing spreadsheet on next tab by JavaScript.

function Dashboard() {
var ss = SpreadsheetApp.openByUrl("Complete URL");
}

Upvotes: 0

Views: 141

Answers (1)

Nikko J.
Nikko J.

Reputation: 5543

openByUrl() document indicates that:

the spreadsheet is NOT physically opened on the client side. It is opened on the server only (for modification by the script).

To open link in new tab, you can copy Stephen M. Harris answer here.

Try this:

function openUrl( ){
  var url = 'insert your spreadsheet url here';
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}

Note: The sheet will open right next to the spreadsheet where the Apps script is connected and this might not work on your first run due to PopUp Blocker, allow the site and re-run the function.

Upvotes: 1

Related Questions