I am developing a Google App Engine Python based Web application.
I need to automatically create a new document in a folder in the Google Drive account of the currently logged user when he/she presses a button.
I am trying to figure out what library to exploit. I am now using the Python Client Library 3.0 to manage the user's calendars but I can't see how to use them to create new docs and set up some initial content.
It would be great if I could use some Javascript library but I am a bit confused about the many different (sometime depcreated) documentation I find on the Internet (i.e. Google Apps Script, Google DocsList, etc.).
You can try below code to create Google doc using Google App Script, you can add text, html content and images.
//Create document in google drive with given name, open the document and returns doc object
doc = DocumentApp.create('digest.doc');
//Get the document body
var body = doc.getBody();
//Insert some text into body
var text = body.editAsText();
text.insertText(0, 'This is sample text...\n');
You can refer this link for API documentation.
Related
I want to check md5 checksum of my uploaded files on google drive (within the same google account).
I wanted to do this using Google Apps Script (script.google.com).
I have read the documentation thoroughly but still don't understand how to get this metadata of files.
I found many examples with using webAPI (v2,v3), but it needs handling OAuth tokens, parsing http responses... why then is there Google Apps Script?
This is my working example that fetches a file id = file name from a folder named 'test', but I want to call md5Checksum attr...
function testDrive(){
var collectionFolder = DriveApp.getFoldersByName('test');
if (collectionFolder.hasNext()){
var itemFolder = collectionFolder.next();
var collectionFile = itemFolder.getFiles();
while(collectionFile.hasNext()){
var itemFile = collectionFile.next();
Logger.log(itemFile.getId(),'=',itemFile.getName());
}
}
}
P.S.: I ever did my own checksum (sha256) function that processing every fetched file blob(binary) but it has limit of ~25mb per blob.
Solution
In the documentation you can see that a File resource includes the md5Checksum in its JSON representation.
Since there is no md5Checksum wrapper for the DriveApp Class you have to use Advanced Google Services. This feature lets you use the same functionalities of the Drive API but through the Apps Script environment. In the Apps Script Editor click Resources>Advanced Google services...>Drive API (v2). The IDE will take care of authorization the first time you run a function.
Here is an example of using the Drive API advanced service to retrieve the md5Checksums of your Drive files:
let files = Drive.Files.list();
for (item of files.items) {
Logger.log(item['md5Checksum']);
}
Note: An MD5 checksum for the content of this file. This field is only populated for files with content stored in Google Drive; it is not populated for Google Docs or shortcut files.
Reference
Drive API
Advanced Google Services
I have an project where I have to render the html form output to an new google docs(word and NOT THE SPREADSHEET !) The only thing what I can find on the internet is about spreadsheet.
The project.
I have an html with form.
I need to get all the data in a new file in google docs when i click on submit.
Can someone show me a way how to solve this.
Hint Links etc etc.
Try following this code:
function myFunction() {
// Create and open a document.
// Replace with your own document name
var doc = DocumentApp.create('Document Name');
var pages = doc.getBody();
pages.appendPageBreak();
pages.appendParagraph("A paragraph.");
pages.appendPageBreak();//push additional data to the next page
pages.appendParagraph("A paragraph 2.");
}
This code will let you create a document and write in the pages. Also read Document Service, this service allows scripts to create, access, and modify Google Docs files. There are alot of features you can use to make your document more presentable.
Now, you connect your form to apps script. Just follow the steps done in this tutorial. Publish your code as web app, then use the web app URL to pass the variable to your script and save the data to the document.
Hope this helps.
I would like to build an "Agreement Generator" for my business that is based on Google Docs Templates. I have a Node.js app that calls the Google Drive REST API v3. Here's how I'd like to do it:
Get a Google Document's raw content
Replace every ${variable_name} with value in that raw content (value is provided from an HTML form)
Update the Google Document raw content
I have been wrapping my head over this for days and I just can't find out how to get a Google Document's raw content.
It seems that all I can get is metadata. Other than that, I was thinking of maybe using the drive.files.export() method to get a Word document (to keep formatting and layout) then read it with Node and replace the values. But then I would have to re-upload that Word file and manually re-convert it to a Google Document from the Drive UI.
So I'm stuck there! Has anybody ever done something like that? Please help :)
What I'm aiming to do is write something akin to the File > Download as > * functionality currently in Google Spreadsheets, but I want it to be in a custom format.
Specifically, I want to turn a spreadsheet of financial transactions into an QIF or OFX file for importing into accounting software. In essence, pushing a button on the UI will download a QIF/OFX version of the currently open spreadsheet.
I have tried the following so far:
Publishing a service (via implementing doGet) that uses ContentService to create the custom file and return it as a download using TextOutput.downloadAsFile(). This works if I call the endpoint directly using my browser.
Tried redirecting the browser to the Service's URL via window.location, but that doesn't seem to be available in the context of the App Script.
Tried using UrlFetchApp.fetch to have the front-end (the spreadsheet) have the browser navigate to the URL for the service. This didn't work either (not really surprising).
So, is this the right approach here? How else can I attack this?
Looks like this is what you have to do.
Create a UiApp
Add a link to the download inside that app.
Show the UiInstance on the spreadsheet
It's a little bit bulky, but it seems the be the cleanest solution.
function downloadAsWhatever() {
var app = UiApp.createApplication();
app.add(app.createAnchor("Download the file now!", "https://script.google.com/macros/s/[...]/exec"))
SpreadsheetApp.getActiveSpreadsheet().show(app.setWidth(300).setHeight(150));
}
You can set the size of the app to whatever you'd like. I chose to set it smaller so it takes up less space on the Spreadsheet screen.
For now the best way to direct a user to a new page is to create an anchor widget and have them click it. We understand that this is not an ideal user experience, but it should suffice for must use cases.
Source
Is there any method to open and visualize with javascript a google spreadsheet?
In other words I would like to create a script in the Google spreadsheet A,which would be able to physically open and select the spreadsheet B. By Looking at Google Script API - I saw that it is possible to use the SpreadsheetApp.openbyID() method;but it doesn't physically open the spreadsheet and select a specific sheet.
Can somebody tell me if this is possible and what API command should be used for the purpose?
You can do it by embed JavaScript to the google spreadshoot page.
For that, you can user userscripts or to developer your an extension for google chrome.
This is one way, from How to access data on different Google Spreadsheet through Google Apps Script?:
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheet1A1 = sheets[0].getRange('A1').getValue();
var sheet2A1 = sheets[1].getRange('A1').getValue();
you can open by sheet name, but using the sheet number is faster because it does not require a network call.