Category Archives: Google Apps

Dreaded Google Apps error: “We’re sorry, a server error occurred. Please wait a bit and try again.”

a-server-error-occurredPeople who use my Insert and Delete Cell Add-on for Google Sheets have been reporting this error.  I was unable to recreate the error until someone shared a spreadsheet with me and I found the culprit: merged cells.

What’s going on?

merged-cellsWhen you insert cells and shift the existing contents down, my Add-on selects the range of cells from your selection to the bottom of the sheet and moves it down.  But if there are merged cells below your selection, the new range doesn’t make sense and an error is generated.  The same thing happens if you try to delete and shift up, or if you try to shift left or right with merged cells to the right of your selection.

Why not catch the error and generate a friendly error message?

I’ve been trying, but this error isn’t triggering the try – catch section of my code.  There also isn’t a way to detect merged cells without trying some strange tricks that would take a lot more time than I have.  I’ve opened a bug report with Google about this.

This error seems to be Google’s version of “We’re not sure what happened, and hope that if the customer tries again it might go away”.

Google Sheets API access with PHP

api-test

Note on 8 February 2017: Google has released the Sheets API v4 and is now providing PHP sample code and improved PHP support for Sheets in the PHP API client.  My series of articles is now largely obsolete.  See https://developers.google.com/sheets/api/quickstart/php

==========================

Google has been sending out messages saying that several of their APIs are going to be discontinued (for example, the Document List API).  As a result, I tried to modify my custom Google Spreadsheet-populating PHP script to use the new Drive API.  After way too much wasted time, I discovered that the Drive API can create sheets and read their metadata, but can’t add rows — for that we can keep using the Spreadsheets API.

I was successful at converting the authentication portion of my script from the old ClientLogin to OAuth with a Service Account (the script runs behind the scenes of a web site and populates a Google spreadsheet that the web site user has no knowledge of).

It wasn’t as simple as I’d hoped because the Google-provided PHP client for the Drive API doesn’t know about the Spreadsheets API.  I had to dig around the PHP libraries to piece together authentication codes and Google_Http_Requests.  Then Google changed the PHP client to use Guzzle, so I recently made that change.

So, if anyone else out there is interested in the combination of PHP, the Spreadsheets API, and OAuth with Service Accounts, this could save you some time.

The code is available in a public Gist at https://gist.github.com/karlkranich/de225928665dc6b83667

Here are the step-by-step instructions to get this to work:

Continue reading

How to Insert and Delete Cells from Google Spreadsheets

[update on 9/24/2014:  This has now been published as a Google Add-on.  Please see this page.  To install the Add-on, just search for “insert cells” when browsing for Add-ons from Google Sheets]

I haven’t been able to find a way in Google Spreadsheets to insert a cell (or cells) in the middle of a spreadsheet and tell the existing content to shift down or to the right.  Similarly, I’d like to be able to select cells to delete, and have them fill in by content shifting up or from the right.

So, I created a Google Apps script to add “Insert Cells” and “Delete Cells” menus to Google Spreadsheets.  Right now, you need to copy the code from github and paste it into the Script Editor in Google Spreadsheets (see the Tools menu).  Hopefully I’ll get the code cleaned up enough to have Google approve it as an Add-on soon!

Watch the video for a demonstration.

[youtube]https://www.youtube.com/watch?v=g5oTm3BeTCk[/youtube]

Google Apps Script to monitor an email box and send notifications

googlemail-128

Here’s the scenario: you have a second gmail account that gets email infrequently, but you need to monitor it.  For whatever reason (company policy, in my case), you can’t forward the email to your main account.

I discovered that I could log into the second Google account and attach a Google Apps Script to a spreadsheet.  The script monitors the gmail account for unread threads.  If it finds any, it sends a message to my main account.  The script has a time-driven trigger that can be set to run as frequently or infrequently as desired.

To use the script, make a copy of this spreadsheet.  Open the spreadsheet and edit the cells that contain the email message parameters.  Then go to Tools – Script editor.  Choose Resources – Current project’s triggers.  Add a time-driven trigger that runs the script as often as you’d like.

Email Notifier trigger

Google Apps Script to find duplicates in a Google spreadsheet

Sometimes you want to find duplicate rows in a Google spreadsheet, but don’t want to examine every column — maybe you want to exclude a timestamp or other columns.

This Google Apps Script takes a list of columns that you care about.  Then it temporarily copies those columns to a new sheet, looks for duplicates, highlights the duplicate rows in red on the main sheet, and removes the temporary sheet.

The script is attached to this Google Spreadsheet. Make a copy of this sheet, or just copy the script.  You can do that by going to Tools – Script Editor and copying the script to the script editor of your own spreadsheet. Then edit the line that defines the important columns. After you re-open your spreadsheet, the “My Scripts” menu will show up. The first time you run the script, it will bring up the permissions dialog. You’ll need to run it again to make it work.

Here’s a video of it in action: