Can I use Google Apps Script to intelligently add page/column breaks based on how much space appended text will take up in a Google Doc? - google-apps-script

I wrote a custom script that takes Google Sheet data and formats it to generate a Google Doc. The sheet data updates automatically and I have to periodically generate a new report for client use. (Overall this relatively seamless -- it has saved me a huge amount of time!)
Unfortunately, I always have to manually format the report afterward, adding column breaks to make sure individual records appear continuously without wrapping to the next column or page. Is there a way to have the script do this for me? So far as I've found, Apps Script doesn't have a way to "figure out" where on the page the cursor is or determine how much room is left before the bottom of the page.

As per Rubén's confirmation in the comments, there is no built-in feature to do what you want. But alternatively, there is a way to do what you want.
This answer shows that similar idea. You can have certain string/keywords that are placed within your source that can define where you would insert your break.
I'm not entirely sure how your data looks and it is hard to provide an exact answer. But if there is a pattern to your data in sheets and you can preemptively define where you can insert your column break, then that can be doable.
Basically, you will need to design how your data is being read (or even modify it) and format it into the document.
If you can provide a sample data and an expected output, then maybe users here can provide you a specific answer that will cater to your exact needs.

Related

Is it possible for a GAS script to lock a Google Sheet so nobody else can alter it until the script is done

I am familiar with the Lock Service but that is only for locking scripts.
I have some code that will "process" a large Google Sheet. My script needs to re-order the rows. I need/want to make it so while the script is running nobody else can change the order. However, I still need another script to be able to append rows.
We use a Google Form for our team's intake. It appends rows to a sheet. I have an hourly job that will go through all the rows/records and "process them". I have a column that stores the last time a record/row was "processed". I want to sort on that column such that the "oldest" records are on top and then start processing from the top down. If the script fails or times out then the next iteration will just start over...
I know I could use getValues or getDisplayValues to get an array and then write the array back but I worry what would happen if someone sorted the rows as it would muck things up when writing the array back.
Is there some way to accomplish my goal? I want to be able to process the records, and maintain row order to avoid breaking my processing.
The way to block a spreadsheet "completely" is by changing the spreadsheet sharing settings. Remove all editors or change them to viewers, once your script finish, change them back as editors. In a extreme case, usa a second account to act as the owner of the critical files / spreadsheets and only use it for this purpose,so you could block your regular account for doing changes to the spreadsheet.
NOTE: A Google Form editResponseUrl could be used to edit the linked spreadsheet.
I'm facing a similar situation but I took a different approach, I'm using an index/key column (you could use the timestamp column) and using the index/key to save each edited row to the right position, then write the whole resulting array in a single operation (by using setValues()). In my case this is simple because I only require values, I'm not worried about notes, data validation, conditional formatting, comments, etc. and there isn't a Google Form linked to my spreadsheet.
Related
Google Spreadsheet -- get sharing permissions by script
Any way to share google docs programmatically?

google apps script to send spreadsheet pdf

I'm very new at this and I think I'm trying to do something a little complicated. Ultimately I'm looking to do the following: I would like to have a spreadsheet that upon a submit button does the following, sends spreadsheet to a specified email address, then increases a specified cell number by 1, then clears data that was entered in specific cells.
I have figured out how to increase a cell on a button push that's as far as I have gotten. Any chance someone would be able to point me in the right direction?
For your emailing you want to use MailApp.sendEmail()
The PDF conversion mentioned in the subject line is also pretty easy to use Spreadsheet.getBlob() or if you want to be a bit more verbose Spreadsheet.getAs('application/pdf')
Generally the GoogleAPI is pretty good at explaining a lot of the detail on how to connect to the google libraries

Autocompletion based on large sheet (2000+ rows)

When using Google Spreadsheets, and you want to use Validation on a cell based on a range of values, you get a pretty nice autocompletion feature that makes data entry much nicer.
My most common application is in an inventory-like situation, where I reference inventory items through some kind of hash code or part number. My frequently used hashes are committed to my brain, but when i need a new part or I need a variation on an old one, I want a little help making sure I have the correct part# selected.
I always find that I want additional row context with my autocompletion, so now I think I want to make a sidebar addon that has smarter searching rules and also includes more contextual data to ensure that I have the part# I meant. Once I am sure of the part#, one button can push the selected result over to the currently active row.
This solution is a bit "heavier" than data validation, but it does exactly what I want.
Assuming that my inventory source is another spreadsheet, what is a good way to set up my Addon-Script Project?
I was thinking that my sidebar would call an HtmlService function that utilizes Cache Service to hold my "hash list" and a few bits of context in memory. I don't think I am looking at a heavy jQuery solution (only to build the autocomplete dialog as I type), but that is really the whole purpose of this question!
Any thoughts on high level project overview? I am fairly new to Apps Scripts in general, especially since the newer API's have been coming out since 2013.
I did exactly that with my Budget Sheets, moved from Data Validation to Jquery's Autocomplete in a sidebar when the number of compositions jumped from 500 to 2.500, and it is a LOT faster, the search is faster than Autovalidation with 100 itens, the logic I use:
Database:
It's base data is in a Spreadsheet, each time it is updated, there's an OnEdit function that will trigger in several minutes a DB update, this is so that the function won't run unecesserary on several times consectively for the same edit.
The DB is then stored in simple text in JSON format on Google Drive, it is a 2MB file generated from the Spreadsheet data, using DriveApp.getFileById(id).setContet(JSON.stringify(myJsonDataFromSpreadsheet)), the file generation and saving takes up to 30 seconds, the file reading is around 4segs.
Sidebar:
Build a normal HTML - remember to use IFRAME option - and serve it, this is all in the docs, you'll have to send data from the HTML to GoogleScript (eg. the part# to insert) via google.script.run, and get back data (eg. the file with all the part numbers) with SuccessHandler in conjunction with google.script.run.
Main function references:
https://developers.google.com/apps-script/guides/html/reference/run
https://developers.google.com/apps-script/guides/dialogs#custom_sidebars
https://developers.google.com/apps-script/reference/drive/file -> First get the file With DriveApp.getFileById(id).

Big numbers in google forms

I use google form to populate some informations about banks. One of the field in form is 20-digit string (30101810100000000777 for example). But when it stores in google sheet it become a different value - 3,01018E+19 and in this case I loose part of information.
Changing column format to text does not have an effect.
Who knows is it any simple way to fix this problem?
Because I see only one way - to write script which will fill the sheet in different way. I mean scirpt on form side trigger onFormSubmit.
The number is still there. If you click on the cell you will see the longer number. The spreadsheet just converts it to exponential notation to make it easier to read. No data is lost.

Use a Google Docs Spreadsheet as a datasource for a dynamic Google Sites webpage

I have a Google Form that feeds a Google Docs Spreadsheet. I'd like to--in turn--have that Google Docs Spreadsheet feed a webpage.
In plainer English, babysitters fill out the form to sign up to be in our community's Babysitter Directory. The spreadsheet houses all of the data. I'd like to code a webpage to pull selected bits of the data for the online directory.
I've tried doing a separate sheet in the spreadsheet, using a QUERY to select the columns that I want to include (and the order in which I want to include them), publishing that sheet to the web, then embedding that sheet into the webpage in an iFrame. And that works.
But even with the QUERY, there are SO many columns that users need to scroll WAY over to the right to see all the data for each babysitter. It's unwieldy.
What would be way better would be if I could break the data for each entry over multiple lines and do some nice formatting for a directory, rather than just a linear spreadsheet. So that, essentially, each babysitter's "entry" in the directory is more than 1 line long. Does that make sense?
If I was working in Office, I would know exactly what to do: use the Excel spreadsheet as the datasource for a Word Mail Merge and I would put move the fields around on the page to make it all look good.
And, to be sure, if I can do this in a Google Doc, then embed the Doc into the webpage, that's fine, too. But I would think there's some way I can do it directly in the Google Site?
Can I?
If anybody has even just a reference page for me to take a look at, I'd appreciate it.
Thanks!
Are you trying to do this in Google Sites? If so, you can embed the entire spreadsheet on the page, but if you only want certain columns, you can try inserting an Apps Script widget on the page.
You need to know how to write a Google Apps Script that will run JS functions and render HTML, here is a tutorial
To create the Script that can run on your page, go to:
More > Manage site > Apps Script > Add new script
Here's also a link to how to interact with Spreadsheet data.
What you want to do is more or less a database interface that uses a spreadsheet as 'data holder', depending on your programming skills it can be quite easy or very hard...
Here is an example of such a webapp, its has probably too many fields and features but the general idea is the same (a quick search tool and a window to show results).
It that what you had in mind ?
If so I can share the code to help you to get started but if you are not familiar with javascript it will probably need too much effort to get through.