I have a Python cron job that pulls urls from sheet1 and populates sheet2 with scraped data from each url. Sheet 2 consists of [url, title, body, image urls]
I have an Old Google Site where I manually take sheet2's contents and create a new post. I want to automate this.
Structurally speaking would Google Scripts be able to pull and publish this dynamically? Lets say every time Sheet2 is updated (or a set interval).
I've worked with gscripts but never touched Google Sites before today.
The answer is probably yes. Apps script can do this. You have a couple of options.
Old Google sites
Option 1
Embed an apps script into the site as an iframe and stream you posts in that.
Option 2
Use either a standalone apps script or one attached to the google site and update the site using the Google Sites API.
New Google sites
Currently there is no apps script API for the new google sites. But you can embed JS HTML etc as a frame in the new sites. You can even use CDN libraries. Which means you can pull data from your sheet using something like AJAX.
Custom built option
The Google Sites API can be accessed anywhere you can handle Oauth. This means you can update Google Site from server-side code or from any client if you can handle Oauth.
Related
There are plenty of examples on how to use an apps script web app to connect HTML forms to google sheets. However all these refer to a scenario where the owner of the spreadsheet/form is the developer, such as this method. The owner/creator of the spreadsheet/form is not dynamic in these cases as far as I can see.
In my use case I want users to create their own HTML Forms based off data from a spreadsheet in their drive and receive responses in that sheet. So we are talking about multiple users creating their own spreadsheet in their own google account/drive instance and connecting that to my external web app to display the form to the users who have the link.
The flow is like this:
User1 creates Google SpreadSheet in their drive
User1 uses our add on to create the form/link
User1 sends the links out
Receiving Users go to the link which leads to my external web app to fill out the form and submit a response
User1 receives responses in another sheet in the same spreadsheet in their drive.
This is repeated for x amount of users.
I'm thinking this will be done through a sheet add on, but I'm not sure how to connect the add on to the external web app. In the link above it is done by creating an apps script web app, but I can't ask the user to do that/programmatically do it for them.
What options do I have?
There needs to be a url to send data to, with a process listening for said data that I can then run a function on.
Hopefully there is some api for this, maybe for drive?
P.S In case anyone is suggesting that I use google forms for this, google forms is great but it is limited in UI options and to a question and answer format. I'm looking to include search/filter functionality for possibly hundreds of list items with a quantity field. So it can't be done in forms.
This should be possible given the following constraints: 1. The form "owner" would need to pass the Sheet IDs of the source and destination to you and, 2. both of those Sheets would need to be publicly readable and writeable.
AppScript is great for manipulating Docs and Sheets you own, but in your case you'll want to use the core Sheets API instead.
Your application which handles responses is probably best implemented using one of the SDKs depending on which language you write it in.
I have to do a little bit of "crowdsourcing" for my work and it would consist in a very simple web app where a user can register/log in, and then be taken to a page where a picture is shown to a user and submit a number.
I'd like to be able to set up a few rules to choose which picture the user will see (so he won't have to answer twice the same question and also to allow some overlap between users to compare their answers).
We can assume that I have a google sheet with a list of images URLs that can be accessed and that I would like the answers to be populated there.
I figured that this tutorial would be a good start since it handles user creation and management and user can submit ideas :
Creating a CRUD Web App with Google Sheets
That being said I'm a bit clueless, I've tried to look for sample scripts deployed as web app but it's hard to know where to start.
I'll appreciate any help !
Google Apps Script is, basically, a javascript environment with a set of libraries that interact with the Google Apps. For instance, to read or store information from/to Google Sheets or Google Docs. If you know javascript, you can create simple applications there. In addition, you can create plugins for Google Applications if you are interested.
A very simple example
Suppose you wanna display an HTML page. You can create an stand-alone script (an script not-bound to a G suite application) and use the content or the HTML services.
You can create a function that sends a simple message to the browser using the ContentService:
function doGet() {
return ContentService.createTextOutput('Hello, world!');
}
Or you can create a function that sends an HTML file in the same project using the HtmlService:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
Once you have created the function, you must deploy the script as a web application.
Save the script as a new version doing File > Manage Versions and Save new version.
Then, publish the app using Publish > Deploy as web app and provinding information about the permissions for the application.
After Google publishes the application, it gives you an URL to access the application,
You may check more in a simple tutorial on producing content with Google Apps Script. In addition, you may check the google guide to use the HtmlService to provide templated content, i.e. where the HTML are templates which data is provided by javascript functions and variables.
Getting started with Google Apps Script
To start, you may check some tutorials in the internet:
Google has some tutorials and a series of Youtube videos.
In addition, you may find multiple resources for Google App Scripts that may help you:
Google has a list of sample projects
Tanakeich has a list of resources for taking advantage of Google Apps Script.
Oshliaer has another list of resources.
There are Google Codelab (tutorials) for Apps Scripts. They include a lab for using Google Sheets as a reporting tool and for creating a Hangouts Chat bot with Apps Script
Developing Google Apps Script projects locally
Although Google Apps Script provides a web interface to create your applications (your scripts), a better idea may be to create the software locally, using more-traditional tools for Javascript.
Clasp is a command-line tool to develop locally Google Apps Script projects.
Google has a codelab that teach you how to use clasp.
I have a single Google Script that I have developed, yet it is a single script that should be placed on several Google Sites pages, I would like to be able to know using the script which website has loaded my script.
How can the script identify which page has loaded it?
On a web app inserted as gadget on Google Sites, to get the name of the site we could use getActiveSite() to get the Site object representing the active site and then getName(), something like the following:
var mySite = SiteApp.getActiveSite();
var mySiteName = mySite.getName();
After reading up a lot on the Google Spreadsheet API I have come to the conclusion that formatting (such as merging cells, changing fonts etc) is only available throught the Apps scripts.
Since we need to create and fill the spreadsheets with data programatically using Java on the back-end I guess I need to somehow either;
link the new sheet to a Apps script that trigger on-load or
create a Apps script that creates the spreadsheet for me.
Anyone knows?
If you want to just "create" the spreadsheet, you don't need a script to load whenever it spreadsheet is opened. It's probably easier to develop a script that runs once and create the spreadsheet for you.
Another tip is to have a template file that you can copy with most of the formatting (if not all) already there. Possibly pending just little things that are related to the real data the new spreadsheet will have.
Edit to answer the question in the title.
No, you can not add a script to an existing spreadsheet programatically, only manually. What you can do is previously set up a template spreadsheet with a script in it and create new spreadsheets by copying this template.
(answering the comment)
You can run a script programatically, but not upload it. To run a script you can deploy it as a web-app and call its url with either a http get or post (will call its doGet or doPost functions, that you must have declared). Also, you could set this script to run on form submit of any spreadsheet-form and just submit a set of answers to this form. At last (that I can think of now) you could just add the script as a library in another Apps Script and call it directly.
(Aug 2016) There is no way programmatic way to link a Google Sheet and Apps Script code other than manually. Based on what it seems you want ("create and fill the spreadsheets with data programatically using Java"), you can now do it without Apps Script.
TL;DR: Above, #Henrique has answered multiple questions and even questions that weren't asked! The good news is that today, we have more answers representing alternate possible solutions to what you're seeking.
It's now possible to "upload" Apps Script code programmatically with the
import/export system, say with Eclipse since you're a Java developer (2013 announcement).
I agree with Henrique's suggestion that if you create a spreadsheet
template, i.e., Excel file, you can use the Google Drive API to
programmatically import/create identical Google Sheets with all your
desired formatting.
"Formatting (such as merging cells, changing
fonts etc)" can now be done outside of Apps Script, as there is a
"new" Google Sheets API v4 (not GData).
In order to use the new API, you need to get the Google APIs Client Library for Java and use the latest Sheets API, which is much more powerful and flexible than any previous API. Here's one code sample to help get you started. If you're not "allergic" to Python, I also made a video with a different, slightly longer example introducing the new API and gave a deeper dive into its code via a blogpost that you can learn from.
Note the v4 API allows you to create spreadsheets & sheets, upload & download data, as well as, in the general sense, programmatically access a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.), but to perform file-level access such as uploads & downloads, imports & exports (same as uploads & downloads but conversion to/from Google Apps formats), you would use the Drive API instead.
I have been publishing scripts from spreadsheets, grabbing the url, pasting it into the insert apps script gadget dialog, and changing the "docs" to "sites" for a while now and has been very successful except for today. Did something change between GAS, Google Spreadsheet, and Google Sites that stopped this process recently?
Update 6/18/12
In response to comment:
Based on Peter's comment, the url change did affect the process. But the change is not specific to the issue posted here. All of the discussion has urls based on "sites" only, and does not discuss published urls from spreadsheets that contain "docs" which does not work with the Apps Script Gadget. The process posted in the question is not a stable process which in turn could not be reliable if there was a change. So a new "workaround" may need to be found in order to use Apps Scripts from spreadsheets as a service on Google Sites.
Really Published Spreadsheet Script Urls should work with Google Sites just as the Google Sites Scripts Urls do.
This may be related to a recent change in the URL format. Please star this open issue for updates.