Unable to publish specific cells in new Google Spreadsheets - google-apps-script

So I've started migrating items over to the the new google spreadsheets to take advantage of some of the limits being lifted.
I've run into a problem with the new spreadsheets not having an option to publish specific cells rather then the entire sheet. Ideally, I just want a specific cell range published so when it's on the site it remains clean and contained without the need to scroll. I've tried to fiddle with the links and the src to match the old code but it doesn't seem to work.
Here is an example of the old code that works:
<iframe width='544' height='249' frameborder='0' src='https://docs.google.com/spreadsheet/pub?key=0AkC6q_6fzB6WdDNGeTk2X1RGNzBJLVhaaWpld19FUlE&single=true&gid=4&range=B1%3AE11&output=html&widget=false'>
The publish dialogue in the old spreadsheet actually has the ability to select specific cell ranges where the new one doesn't appear to have that option.
Here is a sample of the new spreadsheet code that doesn't work:
<iframe width='544' height='249' src="https://docs.google.com/spreadsheets/d/1HqS9DqBK4r7Qtff8Ecy7wKRPsiSUW_03i9x95OwuW74/pubhtml?widget=true&headers=false"></iframe>
Finally here is a fiddle comparing the two: http://jsfiddle.net/blintster/N2qbv/
As you can see the first example is much cleaner.

SOLVED:
This is the code I used for the new google spreadsheets to publish only the cell range I wanted
<iframe width = "431"
height = "21"
frameborder = "0"
scrolling = "no"
src = "https://docs.google.com/spreadsheets/d/1HKjkaqDlCJwycKH4mlPnrZHzx4RMOyY6JXCsmH4/pubhtml?single=true&gid=35&range=D10:M10&widget=false&chrome=false"
style = "overflow: hidden;">
</iframe>
Replace the spreadsheet key with your own (go to Publish to the Web to find your own spreadsheet key)
gid=35 (This is the sheet id, you'll have to find this by publishing a sheet and seeing what number it comes up with)
range=D10:M10 (This is your cell range you want to show - no longer do you need to use a named range)

It is still possible to get specific cells using the feeds for the old Data apis but you then have to do some work in javascript to format the table from the json representation but you do end up with full control of the look. Here is a working feed:
https://spreadsheets.google.com/feeds/cells/19WD1dqLpctWWfDn0Gazs3pJz8OiV_P55QIXZcQ1mznk/od6/public/values?min-row=2&max-row=5&min-col=1&max-col=2&alt=json-in-script&callback=x
There are a few posts around dealing with the json-p eg Use a Google Spreadsheet as your JSON backend

M. Amedeo Tumolillo posted a way to embed iframes from the the new Google Sheets:
http://hellotumo.com/2012/11/28/how-to-embed-specific-cells-when-embedding-a-google-spreadsheet/

Related

AppsScript with Google Sheets: Conditionally load images based on user interaction/when a range is selected

We've got a google sheet with user editable text that will eventually be used within images. We have a web application that generates the images and serves them, so they can be embedded within the IMAGE function provided within Sheets.
This works fine and ends up being something like:
=IMAGE(CONCAT("PREVIEW_TOOL_URL", "?t=", ENCODEURL(previewText(D2))), 3) where 'previewText' is simply formatting the string to pass to the web tool.
However, to avoid overloading the server and generating a ton of requests every single time someone loads the sheet, I'd like to make it so images only load in some condition (i.e. a user presses a button or they highlight the text), is there some way of doing this?
Add a checkbox to E2:
=IF(E2,IMAGE(CONCAT("PREVIEW_TOOL_URL", "?t=", ENCODEURL(previewText(D2))), 3),"Check to preview")
You can also use Apps Script for this!
You will have to start by:
Go to Tools > Create a new script;
Use the following script:
function loadImages() {
let sheet = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("SHEET_NAME");
sheet.getRange("CELL_YOU_WANT_TO_PLACE_THE_FORMULA").setFormula("YOUR_FORMULA");
}
Do not forget to adjust this script such that it meets your needs accordingly.
Insert a new drawing into the sheet;
Attach a script to it by going to the three dots symbol on the drawing; when asked about the name, input loadImages which is the function which we had created previously.
Press the button on the sheet and that's it!
Reference
setFormula(formula).

Google Slides & Apps Script: Unlink all embedded sheet charts

I'm publishing Google Slides containing a lot of embedded charts coming from a Spreadsheet.
I would like to unlink the embedded charts (and thus avoid to get the update button when the data are updated in the spreadsheet).
If google proposes to update all elements at once through the "Linked objects" entry of the Tools menu, there is no option to unlink all in one shot. I would need to go on each chart and select unlink.
So I'm looking now the option of writing a Google Apps Script to do that without success.
I found a similar question on stackoverflow here:
Remove all hyperlinks of a Google Slide using GAS
But the removeLink function does not have any effect on my chart. I still see the chained icon on the top right corner.
Any idea ?
Unfortunately, it seems that in the current stage, there are no methods for directly removing the link to Spreadsheet from Speadsheet. But when the link to Spreadsheet from Speadsheet chart is removed, it is found that the object becomes an image. I thought that this might be used for achieving your goal. So, from this situation, as a workaround, I would like to propose the following sample script.
Sample script:
This sample script converts the Spreadsheet chart to an image on the 1st slide. By this flow, the link to Spreadsheet from Speadsheet chart is removed.
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const charts = slide.getSheetsCharts();
const chart = charts[0];
slide.insertImage(chart.asImage().getBlob(), chart.getLeft(), chart.getTop(), chart.getWidth(), chart.getHeight());
chart.remove();
The flow of this script is as follows.
Retrieve the Spreadsheet chart.
Retrieve the image blob from the chart.
Insert the image blob by copying the size and place of the Spreadsheet chart.
Remove the Spreadsheet chart.
Note:
This sample script copies the size and place. When you want to copy other values, please modify the script.
References:
asImage()
insertImage(blobSource, left, top, width, height)
remove()
Added:
About your additional question as follows.
I think this is a good workaround for charts (such as pie charts, column ... that can be converted as images). Nevertheless, I have some slides where I have some cells embedded. Running this code on this element is displaying an issue. Do you think this also feasible on embedded tables?
The chart is different from the table. So in this case, I think that your additional question is new question.
Your initial question is for removing the Spreadsheet link from the chart. My answer is for your this question. In this case, the table cannot be used. And, in the current stage, unfortunately, there are no methods for removing the link of Spreadsheet from the table. And also, when the Spreadsheet link is removed from the table, the object type is not changed from the table. By this, my workaround cannot be used. But Slides service and Slides API are growing now. So I think that such method might be added in the future update.
So, as the current method, how about reporting your new question to Google issue tracker? By this, the addition of such method might be considered.

is there a way to make a google form response be formatted and sorted on a sheet with multiple pages

I have a google form that sends responses to a google sheet, I want to be able to populate different pages in the sheet depending on a selection from a drop-down menu in the form.
I would also like to format it differently than how the form automatically populates the sheet if this is possible
I have looked for some proper documentation on how to do this but it's hard to find for me, I have tried a sheets formula but this did not work and using google apps script is hard for me since I have no understanding of how it works properly.
=FILTER('Form Responses '!A2:Z,'Form Responses '!B2:B="topic")
=QUERY('Form Responses '!A2:Z,"where B='topic'")
this gives me the no matches are found in filter evaluation error
=query('Form Responses'!A2:H,"Select * Where B='topic'")
Adding this to the seperate pages fixed my problem with the filtering and using templates lets me keep the formatting i wanted, thanks cooper!

How to embed a single Google Sheets cell's text content into a web page?

Background: I have a Google Sheet that automatically manages key organization event dates, but occasionally one has to be manually overridden. I have been duplicating those auto calc'd event dates using PHP on the organization's web page but that doesn't handle it when the event dates are manually changed back in the Sheets database.
Need Statement: I wish to grab the text content of the Google "DateCalc" Sheet, cell "A33" (which contains the corrected next Date) and embed it automatically in a web page sentence that states, "The next event is scheduled for ???." where "???" is whatever is needed for the HTML embed effort for the text content of cell A33.
All the embedding approaches I have tried using the publish to web feature of Sheets, just display a frame with the entire Sheets page content, and I have not been able to find a simple single text display approach, which is what I seek. All the solutions I have found so far are for much more complex situations. I believe I don't need any more code on the Sheet (source) end, just an appropriate "embed code snippet" at the destination end.
I can embed a complete sheet but haven't found a way to embed a simple text string in line with the surrounding text in the sentence.
This web script is the closest I have been able to come...
Our next meeting is on <object data="https://docs.google.com/spreadsheets/d/e/2PACX-1vQq6sXBel4SOW5U_XLnQ1xyyl4P-aK2v1R-5uofUvstMLV89_yCSqX3Lmsy4B7E21gojeG88efGFjZ0/pubhtml?gid=604484136&single=true&range=A29&chrome=false" type="text/html" width="171" height="15"></object>.
but, if the Code Snippet really ran (I changed the file ID to protect the innocent), you would see it doesn't produce an in-line text string but an isolated "block" with the text inside it, as shown below....
The text seems to be an image, not text, that is displaced above the text baseline and has a small gray blob following it that I just can't get rid of.
I'll assume that your server uses a combination of PHP and HTML, so one approach can be to use that PHP code to make a call to the Sheets API to retrieve the data. In the PHP Sheets API Quickstart you can find a example of how to set up a project to achieve a working connection between your server and the API. After you complete the tutorial, you can modify your script to include the following function so you can achieve your original request.
function getNextDate() {
$client = getClient();
$sheetsService = new Google_Service_Sheets($client);
$spreadsheetID = '{YOUR SPREADSHEET ID}';
$cellRange = 'DateCalc!A33';
$serverResponse = $sheetsService->spreadsheets_values->get($spreadsheetID, $cellRange);
$cellValue = $serverResponse->getValues();
if (empty($cellValue)) {
return "NO MEETINGS AHEAD!\n";
} else {
foreach ($cellValue as $value) {
return $value[0];
}
}
}
A totally different alternative is to use a webapp. You can use this tutorial to deploy a webapp that can be later embedded in your webpage to show the following meeting dates.
So there are two free alternatives to accomplish your objectives: using the Sheets API or a webapp; and in this anwser I provided a function for the API option. Please, don't hesitate to offer information for further clarifications or request for help.
Our next meeting is on
.
I built a free, open source, and easy to use service and badge generator called https://cellshield.info. It works out of the box for public Google spreadsheets. What you see above is a live Markdown output version of a badge that is synced from a spreadsheet with the value formatted as what was shown in your example with a formula of TODAY(). The Markdown just makes an image tag which I suppose you can do with anything else that can make an image tag as well.
If you want the yellow or any formatting to to go away, try this:
Our next meeting is on
.
This was done by adding &color=rgba%28255%2C255%2C255%2C0.0%29&style=flat-square to the arguments. The text doesn't match perfectly, it's servicable.
Anyway, I see you went with tabletop but if you or anyone else is just looking to do what you wanted to do, then my service should be sufficient.
For example, it is used for this Game Boy development contest to present the prize pool amount.
If a private spreadsheet solution is needed, one could fork my code, run it on Cloud Run in their Google Cloud Platform account and explicitly share the spreadsheet to the service account on their Google Cloud Platform instance.
Jacques-
Merci beaucoup !
It has been a challenging several days of non-stop effort but you put me on the right track.
Using less than 20 lines of JavaScript code, including one line for the free TableTop.js library call (see https://github.com/jsoma/tabletop#if-your-publish-to-web-url-doesnt-work) and inserting a string in the middle of my HTML sentence, yielded EXACTLY what I was seeking and the solution was INFINITELY SIMPLIER than virtually everything else I found that required multiple libraries, subscription fees, complex system calls, etc.
The hardest part was inserting the JavaScript code into WordPress which my web site is constructed with, as you can essentially only insert plain text into a WordPress page, but with the an added WordPress plugin (Insert Header and Footers) you can put code into a WordPress page.
Many thanks for getting me headed in the right direction, and having a great learning experience along the way. :-)

What is the right way to put a Drive image into a Sheets cell programmatically?

I have a WebApp that collects work site data into a Google Sheets spreadsheet and also collects work site photos into a Google Drive folder that I create for each job. Some, but not all, of the photos must be viewable in a cell in Google Sheets, such that the sheet can be printed as part of a job completion report.
I use Google Picker to upload files to the folder specific to the work site job. I am unsure of the best way to use them from there.
I have had success setting a cell formula such as =IMAGE("hllp://i.imgur.com/yuRheros.png", 4, 387, 422), but only with images pulled from elsewhere on the web.
Using the permalink trick like this =IMAGE("hllp://drive.google.com/uc?export=view&id=0B8xy-TdDgbjiFoOldUlLT091NXM", 4, 387, 422) does not work; I think it won't tolerate the URL redirect that Google does on those links.
The other way I have read about, but not tried yet, is to write the actual blob into the cell. However, I suspect I will lose any control over subsequent formatting of the report.
Perhaps I am going to need to record the image specification in several ways in several cells:
its Google Drive hash key
its dimensions
its alternate location in imgur.com (or similar)
its blob
Is there a programmatic way to get Google's redirected final URL for an image, equivalent to opening the image and copying the URL by hand? Could one trust it for ever, or does it change over time?
Update : I am wrong about =IMAGE("hllp://drive.google.com/uc?export=view&id=0B8xy-TdDgbjiFoOldUlLT091NXM", 4, 387, 422) not working. It is essential that the image be shared to "anyone with the link", even if the owner of the spreadsheet is also the owner of the image.
I am going to go with recording just 1.hash key and 2.dimensions as my solution, but I'd be delighted to know if anyone else has a better idea.
Assuming you get the ID of your image in your drive, you can use a code like below to insert an image in the last row of a sheet (the url is a bit different than usual):
...
var img = DriveApp.getFileById('image ID');// or any other way to get the image object
var imageInsert = sheet.getRange(lastRow+1, 5).setFormula('=image("https://drive.google.com/uc?export=view&id='+img.getId()+'")');// in this example the image is in column E
sheet.setRowHeight(lastRow+1, 80);// define a row height to determine the size of the image in the cell
...
I would suggest that you carefully check the sharing properties of the files you are trying to show : they must be set to "public" of moved in a folder that is also "publicly shared"
I made a two lines script to use the share link of an image in Google Drive.
Go to Tools > Script editor.
Copy, paste the following code
Save
function DRIVE_IMAGE(link){
return link.replace("open?", "uc?export=download&");
}
Using the script :
Copy the Get shareable link of your image in Google Drive (Maybe you need to set the share preference to Public on the web).
Go to a Google sheets cell.
Enter the formula
=IMAGE(DRIVE_IMAGE("COPIED_LINK"))
We build a lot of sheets with images and use the static link available in the google album archive { https://get.google.com/albumarchive/... } rather than the dynamic link in google photos. the link in the archive normally ends in "-rw" which limits view-ability to some with whom the doc is shared with. Deleting "-rw" from the end of the link seems to help.