0

I am trying to take data from a selected range in a Google Sheets spreadsheet and have it wind up in a Google Doc with new formatting. To try and illustrate:

Here is an example of what the data from the Google Sheet would look like:

Name Title Genre Country Year
Name1 Title1 Genre1 Country1 Year1
Name2 Title2 Genre2 Country2 Year2

And here is what the end result would ideally look like in a Google Doc:

Name1 – Title1Genre1 – (Country1, Year1)

Name2 – Title2Genre2 – (Country2, Year2)

I have searched to try and find information for how to accomplish this but all results seem to be focused on:

  • Taking the data from a Google Sheet spreadsheet and creating multiple Google Docs from a template (for, say, a series of marketing letters)
  • Just copying and pasting (as is) a Google Sheet spreadsheet or range into a Google Doc
  • Data manipulation way beyond both my requirements and my comprehension

I have tried to utilize the tools within Google Sheets and Docs (at least as I understand them) and have not been able to ascertain how to make this happen. Looking through Sheets and Docs documentation I'm not finding any help.

I think I'm not using the right terminology in my searches, which is why I'm turning up nothing applicable. So I apologize if this has been answered previously and I'm missing it by looking in the wrong place with incorrect language. If someone could at least nudge me in the right direction, that would be excellent.

Thanks in advance.

2
  • 1
    As this is a custom solution unique for your use case, it'll be hard to find ready made scripts/solutions for this. However, this is entirely doable through Apps script.
    – TheMaster
    Commented Sep 16, 2023 at 15:06
  • Data manipulation way beyond both my requirements and my comprehension Not exactly sure what you mean by this... Would you care to elaborate.
    – Tedinoz
    Commented Sep 17, 2023 at 9:15

1 Answer 1

0

Good afternoon, excuse my English, I use the translator.
I am attaching a script in the absence of more information in order to provide knowledge.

Instrucciones:

  1. Select range Click menu
  2. Tools Click submenu Create document
  3. Accept permissions
  4. Tools Click submenu Create document
  5. Wait for the dialog box to be displayed
  6. Click link open document

Demo:
createDocument.gif

Class Ui
An instance of the user-interface environment for a Google App that allows the script to add features like menus.

Class RangeList
A collection of one or more Range instances in the same sheet.

Class DocumentApp
The document service creates and opens Documents that can be edited.

Class Body
An element representing a document body. The Body may contain

Method appendTable(cells)
Appends a new Table containing a TableCell for each specified string value.

setAttributes(attributes)
Sets the element's attributes.

showModalDialog(userInterface, title)
Opens a modal dialog box in the user's editor with custom client-side content

Code.gs

/**
 * The event handler triggered when opening the spreadsheet.
 * @param {Event} e The onOpen event.
 * @see https://developers.google.com/apps-script/guides/triggers#onopene
 */
function onOpen(){
   SpreadsheetApp.getUi()
    .createMenu('Tools')
    .addItem('Create Document', 'createDocument')
    .addToUi();} /*  End function  */
/**
 * Create new document drive
 */
function createDocument(){
    let style = {};
    style[DocumentApp.Attribute.BOLD]= true;    

    const getValues = SpreadsheetApp.getActiveRange().getValues();
    const classDoc = DocumentApp.create('documentDemo');
    const classBody = classDoc.getBody();
    
    classBody.appendTable([['Name', 'Title',    'Genre',    'Country',  'Year']]);
    classBody.setAttributes(style);
    classBody.appendTable(getValues);

    var htmlOutput = HtmlService
        .createHtmlOutput(`<a href="${classDoc.getUrl()}" target="_blank">${classDoc.getName()}  </a>
                           <span>&ThinSpace;&ThinSpace;</span>
                           <input type="button" value="Close" onclick="google.script.host.close()"/>
                           <br /><br />
                           <iframe src="${classDoc.getUrl()}" width="550" height="400">`)
        .setWidth(600)
        .setHeight(500);        
    SpreadsheetApp.getUi().showModelessDialog(htmlOutput, 'CREATED DOCUMENT');}/*  End function  */  

Greetings waiting for more information

Not the answer you're looking for? Browse other questions tagged or ask your own question.