solutions icon indicating copy to clipboard operation
solutions copied to clipboard

[mail-merge] represent Google sheet formatting

Open rsignell-usgs opened this issue 5 years ago • 7 comments

mail-merge is a cool tool, and will save me a bunch of time. This isn't a huge deal, but would it be possible to represent the formatting in the Google sheet?

For example, I'm working on sending a membership list for a community garden what their fee is, and although I've got "your annual plot fee is {{Fee}}" and the fee is listed in the sheet as $34.30, the email comes through as "your annual plot fee is 34.3".

I could workaround this a bit in the email by doing "your annual plot fee is ${{Fee}}" but I'd still have the issue of dropping the final digit if it's a zero.

I'm hoping there is a better solution.

rsignell-usgs avatar Feb 03 '20 23:02 rsignell-usgs

@mhawksey is this a realistic enhancement request or is the formatting in the sheet not accessible through the API?

rsignell-usgs avatar Feb 05 '20 13:02 rsignell-usgs

@rsignell-usgs I can see how this would be a useful feature as both number and date formats are probably a common usecase. There is actually a relatively easy fix that uses the Advanced Sheets Service which returns formatted values. To use this you would need to enable the Sheets service and instead of:

var data = dataRange.getValues();

use:

var data = Sheets.Spreadsheets.Values.get(SpreadsheetApp.getActive().getId(), 
                                          dataRange.getA1Notation()).values;

a little further down in the code you'd also have to change

o[k] = values[i];

to

o[k] = values[i] || "";

Alternative just copy this modified version of the solution which has the code changes and Sheets Advanced Service enabled.

Let me know if it works and I can make a pull request

mhawksey avatar Feb 11 '20 21:02 mhawksey

I tried this for a sheet that had carriage returns in some of the cells...it returns "String contains control character". Any way around that?

AveryTimm avatar Mar 14 '20 15:03 AveryTimm

@AveryTimm adding:

variableData = variableData.replace(/\n/g, '<br>');

after variableData is declared appears to work

I've updated the modified solution in https://github.com/gsuitedevs/solutions/issues/107#issuecomment-584849047 to include this mod

mhawksey avatar Mar 15 '20 08:03 mhawksey

It works! Thank you...I was just about to find all the ones with returns and send them manually.

I notice that this version will put "NOT_FOUND" in fields that are blank, which many of mine are. It's easy enough to find and replace the NOT_FOUND's, but if they don't need to be there it would be better to leave the original empty cells empty.

Thank you...that saved a lot of time.

AveryTimm avatar Mar 16 '20 10:03 AveryTimm

@AveryTimm ... I see what you mean about NOT_FOUND - I've added a hacky tweak to the code shared in this thread to avoid that

mhawksey avatar Mar 16 '20 15:03 mhawksey

Sheet cell formats picked up in https://github.com/gsuitedevs/solutions/pull/126

mhawksey avatar May 24 '20 13:05 mhawksey