0

I am trying to copy the sheet from one account from another account while accessing google form. I am able to copy the sheet by using below Appscript. But i need to open the same while copying itself. Currently able open by manually.

function myFunction() {

 var app=SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1DpeqCN2Bm2pWxf4TiMEa53xM6MEhSx8GfP6CuyI/edit#gid=0");
 app.copy("NewSheettest19");
}
dspencer
  • 4,297
  • 4
  • 22
  • 43
  • Thnaks. I have tried with suggestion. But i am getting below exception. Exception: Cannot call SpreadsheetApp.getUi() from this context. (line 10, file "Code") for this line SpreadsheetApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... "); – Muthu Marimuthu Apr 14 '20 at 02:03
  • Also i have tried with FormApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... "); instead of SpreadsheetApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... ");. For this i am not getting any exception but still sheet is not opening. – Muthu Marimuthu Apr 14 '20 at 02:08

1 Answers1

0

Opening the file in user context

You can't trigger a script on form submission that runs in the user context. Installed triggers run with the owner's privileges. This could result in security issues. You may email the user with the link or insert it in a shared spreadsheet cell. But you cannot access the user interface in this context.

Please look at this answer for additional information: https://stackoverflow.com/a/26978896.

Opening the file in the editor context:

You won't be able to open the copied file directly from Apps Script. You can use though a workaround building a user interface that uses javascript to open a new page, navigating to your recently copied spreadsheet file.

Code:

var app=SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1DpeqCN2Bm2pWxf4TiMEa53xM6MEhSx8GfP6CuyI/edit#gid=0");
var copied = app.copy("NewSheettest19");
var html = "<script>window.open('" + copied.getUrl() + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html);
FormApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... ");

This code will create a custom dialog containing the Javascript to open the new Spreadsheet and then it will close the dialog.

References:

G Suite Docs Dialogs

Window.open()

Alessandro
  • 2,848
  • 1
  • 8
  • 16
  • Thanks. I have tried with this and this also not working. Can you please help me out. I have tried with suggestion. But i am getting below exception. Exception: Cannot call SpreadsheetApp.getUi() from this context. (line 10, file "Code") for this line SpreadsheetApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... "); – Muthu Marimuthu Apr 15 '20 at 03:19
  • Also i have tried with FormApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... "); instead of SpreadsheetApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... ");. For this i am not getting any exception but still sheet is not opening – Muthu Marimuthu Apr 15 '20 at 03:20
  • Do you have any message about pop-ups being blocked in the Form page? – Alessandro Apr 15 '20 at 07:33
  • Now i am able to open the copied sheet. Here i am facing one issue like while trying via script i am able to open the sheet. But while trying from form not able to open. – Muthu Marimuthu Apr 15 '20 at 09:06
  • Can you please explain further what are you trying to do inside the Form context? Your question is about opening a Spreadsheet link via Apps Script. Does it work? – Alessandro Apr 15 '20 at 09:11
  • Actually, I am trying to open the spreadsheet from form at run time. I have written script for that. While running script i was able to open the sheet. But with the same script i am trying from form it is not opening. – Muthu Marimuthu Apr 16 '20 at 02:01
  • Are you running the function from the script editor or form a custom menu? It works from the editor, but you will have to enable the popups. – Alessandro Apr 16 '20 at 11:15
  • Yes, when i tried from script editor it is working fine. But i need get the same while access via Form. – Muthu Marimuthu Apr 17 '20 at 01:57
  • Please rephrase this: "But i need get the same while access via Form." I cannot identify the corresponding action. Do you want to open it when you open the Form? Do you want to open it from a custom menu button in the Form? Is it another action? Thank you! – Alessandro Apr 17 '20 at 08:29
  • I have submit button in form. While clicking submit button sheet should be open. – Muthu Marimuthu Apr 20 '20 at 02:12