0

[Goal]

I want to be able to copy a table from a Spreadsheet and paste it to a Google Docs agenda document that is used for team meetings. The exact steps are as follows.

  1. The user will use the custom menu on the Google Docs agenda file to run the script. enter image description here
  2. The script will copy the all the body content from another separate Google Docs template file.
  3. It'll then paste everything starting from the 4th line from the top of the agenda file. Example: enter image description here
  4. The script will copy a table from a Spreadsheet (in this example, range A5:G17). enter image description here
  5. The script will then paste the table into the agenda file in the exact location. Example: enter image description here

[Issue]

However, the copied table from the spreadsheet executed by the script isn't properly formatted in the agenda template. Here are the examples.

What I DON'T want: This is done through the script. enter image description here

What I do want: I had to manually copy the table from the spreadsheet and paste it manually to the agenda doc. However, I'd prefer having the script do it. enter image description here

[My Code so far]

function onOpen() {
  initMenu();

}

function initMenu() {
  const ui = DocumentApp.getUi();
  const menu = ui.createMenu('Custom Functions');

  // updateMainData function
  menu.addItem('Add New Week', 'addNewWeek');
  menu.addToUi();

}

function addNewWeek() {
  const doc = DocumentApp.getActiveDocument();

  const templateDoc = DocumentApp.openById("12COMKRpQT-sut-esvYR9B8KlrSptr5JcwP6Min2uxMU");
  const templateBody = templateDoc.getBody();
  var templateTotalElements = templateBody.getNumChildren();

  // Modifying Tempalte Date
  var index1 = templateBody.getChild(1).getText();
  const templateDate = new Date(templateBody.getChild(1).getText());
  const incrementDate = new Date(templateDate.setDate(templateDate.getDate() + 7));
  const convertDateFormat = Utilities.formatDate(incrementDate, "JST", "yyyy-MM-dd");
  templateBody.replaceText(index1, convertDateFormat);

  for (var i = 0; i < templateTotalElements; i++) {
    var body = doc.getBody();
    var element = templateBody.getChild(i).copy();
    var type = element.getType();

    if (type == DocumentApp.ElementType.PARAGRAPH) {
      body.insertParagraph(3 + i, element);

    } else if (type == DocumentApp.ElementType.TABLE) {
      body.insertTable(3 + i, element);

    } else if (type == DocumentApp.ElementType.LIST_ITEM) {
      body.insertListItem(3 + i, element);

    }

  }

  const mtgSheet = SpreadsheetApp.openById("1MMSGNt4tPZmTLEsOMNA9i1TI0duY88Ey9gJHMq0G9sU").getSheetByName("Sheet1");
  const mtgRange = mtgSheet.getRange("A1:G13").getDataRegion(SpreadsheetApp.Dimension.ROWS);
  const mtgValues = mtgRange.getDisplayValues();
  const mgtBackgroundColors = mtgRange.getBackgrounds();
  const mtgStyle = mtgRange.getTextStyles();

  var body = doc.getBody();
  var table = body.insertTable(8, mtgValues);
  table.setBorderWidth(0);

  for (var i = 0; i < table.getNumRows(); i++) {
    for (var j = 0; j < table.getRow(i).getNumCells(); j++) {
      var obj = {};
      obj[DocumentApp.Attribute.BACKGROUND_COLOR] = mgtBackgroundColors[i][j];
      obj[DocumentApp.Attribute.FONT_SIZE] = mtgStyle[i][j].getFontSize();
      
      if (mtgStyle[i][j].isBold()) {
        obj[DocumentApp.Attribute.BOLD] = true;

      }
      table.getRow(i).getCell(j).setAttributes(obj);

    }

  }

}

[References that I found]

I tried using the methods listed here, which got me very close, however, I don't know what to do next.

Solana_Station
  • 187
  • 1
  • 14
  • 1
    First, I apologize that my answer was not useful for your situation. About your question, in the current stage, it seems that the column width of the Spreadsheet is not the same as the table of documents. [Ref](https://stackoverflow.com/a/59710473) On the other hand, I think that the font color can be copied. On the other hand, I think that the font color can be copied. In this case, what do you want to do about `the width` of `the width and the text color aren't matching`? – Tanaike Dec 19 '22 at 13:37
  • No need to apologize Tanaike-san. Thank you for checking this! I'm not that concerned about the width of the columns. I mainly want the text colors, borders, background colors, alignment to be the same as source (spreadsheet). Please let me know if this doesn't clarify things. – Solana_Station Dec 19 '22 at 14:05
  • @Tanaike -san, if there's anything that you need clarification with, please let me know. – Solana_Station Dec 20 '22 at 01:10
  • 1
    Thank you for replying. When I could correctly understand your question and I could find the solution, I would like to answer it. – Tanaike Dec 20 '22 at 02:31
  • @tanaike Hello Tanaike-san. I'm very sorry if I've misunderstood your comment. However, do you think it's possible to achieve what I've described above? I haven't heard from you yet while I thought you were going to help with the solution. Hope this can be solved because I really need it... :( – Solana_Station Jan 16 '23 at 03:08
  • 1
    Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill. – Tanaike Jan 16 '23 at 05:00
  • @tanaike -san, thank you for your kind feedback! I understand that my question was definitely confusing, so I went ahead and edited it. Can you kindly check it once more and see if it makes sense now? I sincerely thank you for your support!! – Solana_Station Jan 16 '23 at 14:06

0 Answers0