0

I have been trying to do this, but seem to be unable to comprehend the whole picture.

I have a spreadsheet augmented with custom scripts that goes around the internet and collects stuff. I have added a new menu - 'download source data', which is intended to allow the user (me in this case) to have 'one click' download of the raw data in csv format. The problem is that the default export in csv is not doing all I need and I prefer to create the content of the file in script. I use ContentService. When I press the menu it goes and creates what is needed, however I would expect the script to trigger a download in the browser, but this does not happen.

So my question is: is it possible to do this from a menu (ActiveSpreadsheet.addMenu and there reference to my function). If yes, how do I tell the browser to start downloading the file with the contents of the created TextOutput. I have tried downloadAsFile('filename') but it does nothing. No errors are displayed.

Peter StJ
  • 2,297
  • 3
  • 21
  • 29
  • The TextObject seem to be of use only in the 'goGet' function and only if the script is published as an App. This is fine, I managed to do that. Separately in the spreadsheet app UI is created to show the link to the app, which on theory, when loaded in the browser should trigger the download. This only works when I deploy the app and press 'Test your latest code'. When opened from the UI it gives Script function not found: doGet. How come? – Peter StJ Apr 05 '14 at 16:30
  • Just a quick note: I got to the point where everything works except the fact that opening a new browser tab or navigating the current URL in the browser seem to be impossible. Having to click a menu and then to click a link seems WRONG. I am aiming at a feature similar to what sheets have by default - download as... you click it and the download starts. How do I achieve this (i.e. I already have the web app working, I just need to triger browser navigation to it from a menu item...... – Peter StJ Apr 05 '14 at 16:54
  • Thanks for the downvote, it's always a pleasure. You won't find a way to do what you want ie have a menu item that directly redirect to an url, it will we a 2 step process. – Serge insas Apr 05 '14 at 17:33
  • Sorry you feel that way bout the vote. To tell the truth the downvote was for sending me in completely different direction without mentioning the most important part - that it is not possible to open url from menu item at all (in web dev user clicking menu is user action). Content service is capable enough to create a file and force the browser to downloaded should the correct url is opened. So DriveApp is not needed. This is why the downvote. – Peter StJ Apr 06 '14 at 18:48

2 Answers2

0

DriveApp has a getDownloadUrl() method that you can use to show an url in a small popup Ui. You'll have to click on the url to start the download as there is no way to follow a link without user action.

See this other post for an example of such a Ui, this is a standard url but the approach is exactly the same.

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • The proposed solution assumes I have written the TextObject as a file in Drive, which I have not. The main idea is to generate the content on the fly and return it to the browser as csv. – Peter StJ Apr 05 '14 at 16:25
  • 1
    Why not create the file in drive once you have the string content ? you can delete it right away if you want. – Serge insas Apr 05 '14 at 16:30
  • 1) How would I trigger download from Spreadsheet menubar item 2) how would I know when the download completed from app point of view so I can delete the file? -- using UI just to show URL that has to be clicked is not good UX. I am aiming at 'Select menu item -> trigger download'. This still counts as user action - right? – Peter StJ Apr 06 '14 at 10:58
  • 1-you can't. 2-once the UI is closed you know that the file can be deleted since every new call would create a new unique file. – Serge insas Apr 06 '14 at 14:04
  • Thanks. I do not understand the following: assuming the file is a large one I can click the link on the UI, it will start downloading and then the user closes the UI, is it safe to delete it now. It might still me loading... – Peter StJ Apr 06 '14 at 18:38
  • Delete in this context is actually move to trash, it means that the file will still be available. You can't empty the trash using script so it should not be a problem. – Serge insas Apr 06 '14 at 20:37
0

So the answer of the question is no: it is not possible to trigger file download by the user when clicking a custom made menu. Which is a shame:)

Thanks to Serge insas who had to be offended first and only then considered answering:)

Peter StJ
  • 2,297
  • 3
  • 21
  • 29