New Doc Created from Spreadsheet Data Script

35 views
Skip to first unread message

Ad Account

unread,
Apr 9, 2019, 1:21:09 PM4/9/19
to google-apps-sc...@googlegroups.com

Hello, I have fairly basic experience scripting within the google environment, and am wondering if someone can help me with the text fill section of my project.

Basically I have already created the script for copying a template and saving it as a new document.

Now I need to know how to copy the cell data so it inputs it into the google doc in the spaces I need it to populate. If someone could give me the command to this scripting, or help me out by writing an example, that would be extremely helpful.

Thanks!

Riël Notermans

unread,
Apr 9, 2019, 2:28:36 PM4/9/19
to Google Apps Script Community
You can do things like:

DocumentApp.getBody().replaceText("replace","replacewith")



On Tue, Apr 9, 2019 at 10:21 AM Ad Account <sd.a...@gmail.com> wrote:

Hello, I dont really have any experience scripting within the google environment, and am wondering if someone can help me with the text fill section of my project.

Basically I have already created the script for copying a template and saving it as a new document.

Now I need to know how to copy the cell data so it inputs it into the google doc in the spaces I need it to populate. If someone could give me the command to this scripting, or help me out by writing an example, that would be extremely helpful.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4e0c38ab-d213-448a-b30f-5c89fa637b5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Clark Lind

unread,
Apr 11, 2019, 9:41:01 PM4/11/19
to Google Apps Script Community
Get your data from the sheet with something like:
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1'); //your sheet name
  var data = sheet.getRange('A2:D2').getValues(); //your cell range

The data will now be in a 2-dimensional array:  [[A2-value, B2-value, C2-value, D2-value]]

In my Doc templates, I use named fields. Like {firstName}  {lastName} {phone#} {Email} for example.

Call your Doc, and replace the fields with the data:
var Doc = DocumentApp.openById(##YourDocId##); //if making a copy from a template, grab the new file's Id upon creation and save to a var. Then use that var here.
  Doc.replaceText('{firstName}', data[0][0]);
  Doc.replaceText('{lastName}', data[0][1]);
  Doc.replaceText('{phone#}', data[0][2]);
  Doc.replaceText('{Email}', data[0][3]);

This could be cleaned up a lot and simplified, but that should give you an idea of how to do it.
Reply all
Reply to author
Forward
0 new messages