Does anybody know if there is a new limit for attaching files in a Google Site page with aps script ?
A simple example doesn't work anymore for large files :
Steps to reproduce :
1.Create a UiApp with the code below
2.Run the script in a Google Site
3.Select a small file (<1MB)
4. Result is ok, file attached.
5. Run again with a larger file (>2.6MB),
6. Result is NOK :"error encountered : an unexprected error occurred"
Source code :
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload File to Site");
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton("Click to upload"));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
function doPost(e) {
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.thefile;
SitesApp.getActivePage().addHostedAttachment(fileBlob);
}
This worked before, not since a few weeks.
This was is a known change in behavior and the team is working on resolving this.
Here is the Issue Tracker item to check -
https://code.google.com/p/google-apps-script-issues/issues/detail?id=2723
Related
I am a beginner to apps script so we're probably not off to a great start but here I go...
I am in the process of creating a simple dashboard using cool (but recycled) code from the google team.
Whilst researching ways of improving the way the dashboard displays, I found out that the UI service is being deprecated (I first had to research what deprecated meant).
So now I realise I need to use the HTML service for my dashboard. The question is how do I convert the code below so it works on the HTML Service?
The code often refers to the variable 'app' which refers to the UI Service. I'm not sure if I just need to update that part or if I need to create a HTML file and start again. I hope someone can help me.
function doGet() {
var ss = SpreadsheetApp.openById('1EVpJrmuo7Er_a3Xsoy09VBBfQjEt5Ihdmm3JAWsPTIA');
var data = ss.getDataRange();
var ageFilter = Charts.newNumberRangeFilter().setFilterColumnIndex(4).build();
var transportFilter = Charts.newCategoryFilter().setFilterColumnIndex(2).build();
var statusFilter = Charts.newCategoryFilter().setFilterColumnIndex(7).build();
var monthFilter = Charts.newCategoryFilter().setFilterColumnIndex(0).build();
var nameFilter = Charts.newStringFilter().setFilterColumnIndex(1).build();
// Create a table and pie chart
var tableChart = Charts.newTableChart()
.setDataViewDefinition(Charts.newDataViewDefinition().setColumns([1,2,3,4]))
.setDimensions(1000,500).build();
var pieChart = Charts.newPieChart()
.setDataViewDefinition(Charts.newDataViewDefinition().setColumns([1,4]))
.build();
// Create a dashboard to bind the filters to the charts.
var dashboard = Charts.newDashboardPanel().setDataTable(data)
.bind([ageFilter, transportFilter, statusFilter, monthFilter, nameFilter], [tableChart, pieChart])
.build();
// Create a UiApp to display the dashboard in.
var app = UiApp.createApplication();
var filterPanel = app.createVerticalPanel()
.add(app.createLabel("Fix Tickets"))
.add(app.createHorizontalPanel()
.add(app.createVerticalPanel()
.setSpacing(10)));
var chartPanel = app.createHorizontalPanel();
filterPanel.add(ageFilter).add(transportFilter).add(statusFilter).add(monthFilter).add(nameFilter).setSpacing(10);
chartPanel.add(tableChart).add(pieChart).setSpacing(10);
dashboard.add(app.createVerticalPanel().add(filterPanel).add(chartPanel));
app.add(dashboard);
return app;
}
I had the same problem as you. Recently came across this. Maybe it will help.
Converting UiApp to HTML
I want to show a preview of a file (located on Google Drive) selected from a list or tree.
If I try to display an image file the image doesn't show up (allthough it is the right fileId)
function doGet()
{
var app = UiApp.createApplication().setTitle("Image");
var urlDrive = 'https://drive.google.com/file/d/0BxjtiwHnjnkrUVFKaWVaM3BNZjg';
var urlWeb = 'http://cdn.ndtv.com/tech/images/gadgets/google_webfonts_ap.jpg';
// var url = urlWeb; // works
var url = urlDrive; // Doe NOT work
var image = app.createImage(url).setHeight(200);
var panel = app.createVerticalPanel();
panel.add(image);
app.add(panel)
return app;
}
The example shows that changing the url to a file not present on Google Drive it works.
In general I would like to know if it is possible to preview a file (including msWord, msExcel and pdf) in a panel using GAS. A small example will be appreciated much of course.
The problem is the way you get the url.
The only url that works in this case is the one you can see when you examine the image file with "details" in your drive browser window.
it is actually build differently but contains the ID so it is quite easy to re-build...
Here is how it goes:
function doGet(){
var app = UiApp.createApplication().setTitle("Image");
var imgFile = DriveApp.getFilesByName('Chrysanthemum.jpg').next()
ID = imgFile.getId();
Logger.log(ID);
var imgUrl = 'https://googledrive.com/host/'+ID
var image = app.createImage(imgUrl).setHeight(200);
var panel = app.createVerticalPanel();
panel.add(image);
app.add(panel)
return app;
}
test here
EDIT :
Since the code above seems not to work for files that are not publicly shared (not sure) here is another way to get the result (test app updated) that works with files that are shared to 'anyone with the link can view'.
Note That this is not logical since the app is deployed to run "as me" and is accessible to anyone even anonymous"... so from the G Drive pov I open the file... go figure why it behave like that ;-)
I tested in an anonymous session and it works.
I hope this solution will be suitable for you.
function doGet(){
var app = UiApp.createApplication().setTitle("Image");
var imgFile = DriveApp.getFilesByName('Chrysanthemum.jpg').next()
var ID = imgFile.getId();
Logger.log(ID);
var imgUrl = 'https://drive.google.com/uc?export=view&id='+ID
// var imgUrl = 'https://googledrive.com/host/'+ID; //for public files apparently
var image = app.createImage(imgUrl).setHeight(200);
var panel = app.createVerticalPanel();
panel.add(image);
app.add(panel)
return app;
}
I'm looking for how to get the file name by the FileUpload just by the file selected before a submit button pushed. But it seems not allowed.
I think the following code can probably hold up the FileUpload changed.
function doGet()
{
var app = UiApp.createApplication();
var panel = app.createVerticalPanel().setId('panel');
var fileUploader = app.createFileUpload().setName('thefile');
var handler = app.createServerHandler('fileChangeHandler');
handler.addCallbackElement(panel);
fileUploader.addChangeHandler(handler);
panel.add(fileUploader);
var form = app.createFormPanel();
form.add(panel);
app.add(panel);
return app;
}
But I don't know how to grab the file name in fileChangeHandler(). For example, the following is a failed code:
function fileChangeHandler(e)
{
var app = UiApp.getActiveApplication();
Logger.log(e.parameter.thefile);
return app;
}
Thanks very much for your comments ^^
You cannot use a regular button for FileUpload, you must use a SubmitButton. See these two answers.
GAS: GUI Builder, File Upload, Submit Button
File upload at google apps script
I try to upload file to my 'mysitename' Google Site. I using this script, but it does not work
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload");
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton());
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
return app;
}
function doPost(e) {
try{
var fileBlob = e.parameter.thefile;
var pages = SitesApp.getSite('site', 'mysitename').getChildren();
var attachments = pages[0].getAttachments();
attachments[0].setFrom(fileBlob);
}catch(e){
Logger.log(e.message);
}
}
I catch error "Cannot call method "setFrom" of undefined" last modal window "Error encountered: An unexpected error occurred"
It's works! But i do not know, how to use this circular from documentation "Sets the data, content type, and name of this attachment from a Blob. Throws an exception for web attachments." What is mean this "Throws an exception for web attachments"? I'll be forced to use "try-catch-exception"
Pls, help me to do it!
I think the error is in the line
var pages = SitesApp.getSite('sites', 'mysitename').getChildren();
The documentation says
method getSite(domain, name)
Gets the site with the given domain and name.
Use "site" as the domain for consumer sites.
So, if you are within a domain, use the name of your domain, otherwise use 'site' instead of 'sites'
New to GAS and am struggling with my first script. Any help will be appreciated.
The UI displays but I cannot write data in a xls file. It was working when I was using a handler, however, I had to change it to a doPost to get file upload to work.
I want to have UI that has several fields and 2 file upload options. Does anyone have sample script that I can use to get started?
My script
function doGet(e)
Create a vertical panel with a grid that has text boxes
Create a form that has a file upload box
function doPost(e) {
var doc = SpreadsheetApp.openById('0Auxvh5UNOiURdE1uc1VOVGNlTE01bjF4THRmdzFnSXc');
var lastRow = doc.getLastRow();
var cell = doc.getRange('a1').offset(lastRow, 0);
cell.setValue(e.parameter.universityname);
cell.offset(0, 1).setValue(e.parameter.eventname);to age
cell.offset(0, 2).setValue(e.parameter.city);
var fileBlob = e.parameter.thefile;
var doc = DocsList.createFile(fileBlob);
var app = UiApp.getActiveApplication();
app.close();
return app;
}