convert document to google format using google.apps.scritp - google-apps-script

I can't find the way to convert the documents uploaded by my google script into the native Google Docs format.
In Drive configuration options, you can set if the uploaded document will convert automatically into Google Docs format. But when i do this in my google apps sript with the DocList.createFile() method, the convertion is not maked.
This is my script
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
var formContent = app.createVerticalPanel();
form.add(formContent);
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
app.add(form);
return app;
}
function doPost(e) {
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.thefile;
var doc = DocsList.createFile(fileBlob);
var app = UiApp.getActiveApplication();
// display a confirmation message
var label = app.createLabel('file uploaded successfully');
app.add(label);
return app;
}

Related

Generate and download pdf file using google apps script to local PC

I wrote a Google Apps script in a Spreadsheet that convert the Spreadsheet to pdf. This works.
How can I copy this generated pdf file automatically using the same script to PC's local folder (Desktop f.e.)
Code.gs
function onOpen() {
var submenu = [{name:"Save PDF", functionName:"generatePdf"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Export', submenu);
}
function generatePdf() {
var sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var pdfName = Browser.inputBox("Name of generated pdf");
var save2folder = DriveApp.getRootFolder();
var theBlob = sourceSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var pdfFile = save2folder.createFile(theBlob);
}
Thanks

Uploaad image button in google form

I created one google-form in that google-form i want user to upload their image. so i need a upload button to put their passport size image. i have written an separate code to upload image. where i can upload image the image comes and saved in drive. there are 2 pieces. i want this to merge or combine . i want to include a upload button image in the form and the uploaded image should come and save in the form spreadsheet. thanku
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
var formContent = app.createVerticalPanel();
form.add(formContent);
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Submit'));
app.add(form);
return app;
}
function doPost(e) {
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.thefile;
var doc = DocsList.createFile(fileBlob);
var app = UiApp.getActiveApplication();
//Display a confirmation message
var label = app.createLabel('file uploaded successfully');
app.add(label);
return app;
}
It's not possible to merge this function to a Google form.

Form Submit google cloud print

I could use some help with creating a script when a Google Form is submitted.
Once the form submitted, the script automatically prints a Google Spreadsheet to a Google Cloud-based printer without bringing up the print dialog window.
I've read through Google Cloud Print Info
The idea behind the script is simple:
User fills out form
Form is then submitted
Script updates cells with information user put in
Script then prints to google cloud printer
Shipping label is ready
The below script will print a ticket but how do i add the printer capabilities to the POST request?
The code I have so far:
function onsubmit(e) {
var pid = "123456789"; //place printer id here
var sheetid = "123456789"; //place sheet id here
var ss = SpreadsheetApp.getActiveSpreadsheet();
var info = ss.getSheetByName('Form responses 1');
var lastRow = info.getLastRow();
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('TICKET'))
authorize();
var url = "https://www.google.com/cloudprint/submit?printerid="+pid+"&contentType=google.spreadsheet&content="+sheetid+"&title=PrintingTicket";
var options = {
method: "post",
oAuthServiceName : "print",
oAuthUseToken : "always"
};
info.hideSheet();
var response = UrlFetchApp.fetch(url, options);
return Utilities.jsonParse(response.getContentText());
}
function authorize() {
var oauthConfig = UrlFetchApp.addOAuthService("print");
var scope = "https://www.googleapis.com/auth/cloudprint";
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
}

First Google script - save data in xls and file upload

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;
}

exporting spreadsheet to pdf then saving the file in google drive

I'm having problems saving to google drive after printing a google spreadsheet to pdf.
If i just put the "printurl" string into a browser, it will automatically give me the file. But I want it to be saved to google drive automatically. I've tried this code by borrowing code from other posts that emails a spreadsheet as PDF. But this produces a pdf that is unable to be opened. What am I doing wrong?
function printpdf() {
var spreadsheet_id="0Aiy1DTQRndx6dDRidXoxNzlXZFhxd2FITTlBbnUybnc";
var settings = '&fitw=true&portrait=false&exportFormat=pdf&gid=0&gridlines=false';
var printurl = 'https://spreadsheets.google.com/feeds/download/spreadsheets/Export? key=' + spreadsheet_id + settings;
var result=UrlFetchApp.fetch(printurl);
var content=result.getContent();
var file=DocsList.createFile("temp",content,"application/pdf");
}
Here is an update to this question under the new oauth2 format.
Printing spreadsheet to PDF then saving file in Drive using OAuth2
You can do it in a much simpler fashion
function printpdf(){
var spreadsheet_id="0Aiy1DTQRndx6dDRidXoxNzlXZFhxd2FITTlBbnUybnc";
var spreadsheetFile = DocsList.getFileById(spreadsheet_id);
var blob = spreadsheetFile.getAs('application/pdf');
DocsList.createFile(blob);
}
Note that the DocsList.createFile(blob) works only with Google Apps accounts.
did you mean it like that?
var id = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = getConfig(SHEET_NAME_CELL);
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
if (!dataSheet) {
Browser.msgBox("Can't find sheet named:" + sheetName);
return;
}
var dataSheetIndex = dataSheet.getSheetId();
//this is three level authorization
var oauthConfig = UrlFetchApp.addOAuthService("google");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
//even better code
//oauthConfig.setConsumerKey(ScriptProperties.getProperty("consumerKey"));
//oauthConfig.setConsumerSecret(ScriptProperties.getProperty("consumerSecret"));
var requestData = {
"method": "GET",
"oAuthServiceName": "google",
"oAuthUseToken": "always"
};
var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=" + id + "&gid=" + dataSheetIndex + "&fitw=true&size=A4&portrait=true&sheetnames=false&printtitle=false&exportFormat=pdf&format=pdf&gridlines=false";
//Save File to Google Drive
var seplogoBlob = UrlFetchApp.fetch(url, requestData).getBlob().setName("Filename.pdf");
DocsList.createFile(seplogoBlob);