Making Changes to Google Docs Table

62 views
Skip to first unread message

kelo...@sewanee.edu

unread,
Sep 19, 2023, 4:41:41 PMSep 19
to Google Apps Script Community
Hi Everyone,

The purpose of my project is to create a . pdf company directory with a specific format.  I have a csv file that is being dropped into GoogleDrive. I am importing the data from that csv file into a GoogleSheet. I then have a script pulling the data from the GoogleSheet into a table in GoogleDocs. In the table, I have a few criteria: 1. I want to hide the borders of the table, 2. the values in columns 1 and 2 should only appear once in each column (e.g. if I have 20 people in Athletics, the word Athletics in column 1 should only appear once such that all of the employees in the department appear to be nested under Athletics. If I have 5 people in Athletics (Column 1) Administration(Column 2),  the terms Athletics and Administration should only appear once in each respective column),  and 3. if the cells in columns 2, 3, or 4 of a row are empty (there is no value), I want to change the bottom border to 1pt dotted. 

I am encountering two issues. First, my script for recognizing unique values seems to only work for the first page or so. It then hides all data from column 1 for the remaining rows. I am not sure why it is failing. Second, I cannot get the setBorder function to work. properly. I receive TypeError: cell.setBorder is not a function

Any help that you can provide would be greatly appreciated. Of course, if there is a more efficient way to achieve my goal, I welcome your suggestions. Here is my code in it's entirety: 

function importDataFromSheetToDoc() {
var sheet = SpreadsheetApp.openById("XX");
 
var doc = DocumentApp.openById('XX');
 
var sheetData = sheet.getSheets()[0].getDataRange().getValues();
 
var body = doc.getBody();
 
var table = body.appendTable([]);
 
 able.setBorderWidth(0);
 
  // Initialize arrays to keep track of unique values in columns 1 and 2
  var uniqueColumn1Values = [];
  var uniqueColumn2Values = [];
 
  // Set the batch size for processing
  var batchSize = 100;
 
  // Loop through each row in the sheet data
  for (var i = 0; i < sheetData.length; i++) {
    var rowData = sheetData[i];
   
    // Check if column 1 value is unique
    if (uniqueColumn1Values.indexOf(rowData[0]) === -1) {
      uniqueColumn1Values.push(rowData[0]);
    }
   
    // Check if column 2 value is unique
    if (uniqueColumn2Values.indexOf(rowData[1]) === -1) {
      uniqueColumn2Values.push(rowData[1]);
    }
   
    // Add a new row to the table and include all columns
    var newRow = table.appendTableRow();
    for (var j = 0; j < rowData.length; j++) {
      newRow.appendTableCell(rowData[j]);
    }
   
    // Check if any cell in columns 1 or 2 is empty and set the background color to white
    if (rowData[0] === '' || rowData[1] === '') {
      for (var k = 0; k < 2; k++) {
        if (rowData[k] === '') {
          newRow.getCell(k).setBackgroundColor('#ffffff'); // White color
        }
      }
    }
   
    // If a duplicate value is found in columns 1 or 2, clear the cell's content
    if (uniqueColumn1Values.indexOf(rowData[0]) !== i) {
      newRow.getCell(0).setText('');
    }
    if (uniqueColumn2Values.indexOf(rowData[1]) !== i) {
      newRow.getCell(1).setText('');
    }

  if (rowData[1] === '' || rowData[2] === ''|| rowData[3] === '') {
      // Set the bottom border of the cell to 1pt with dots
      var cellIndex = rowData[1] === '' ? 1 : 3;
      var cell = lastRow.getCell(cellIndex);
      cell.setBorder(null,null,true,null,null,null,"black",SpreadsheetApp.BorderStyle.DOTTED);
        // Check if we have processed a batch and sleep briefly to avoid script execution time limits
    if ((i + 1) % batchSize === 0) {
      Utilities.sleep(500); // Sleep for 500 milliseconds
    }
  }
}
}

Thank you!

Kerline

Keith Andersen

unread,
Sep 19, 2023, 5:06:05 PMSep 19
to google-apps-sc...@googlegroups.com
May I ask how many people are in this company directory?

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/c88c9394-4cf2-4a5e-ae0d-3e3360e4e6f2n%40googlegroups.com.

Kerline Lorantin

unread,
Sep 19, 2023, 5:18:07 PMSep 19
to google-apps-sc...@googlegroups.com
As of today, I have 964 lines. I've been using a partial directory with 198 lines for testing purposes.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/GI8yTrlZifQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAJ8MD4wPb93cC7Os2edYLxmFsHg1mKL%3Da7LBUvOYrmMyH_5Z4g%40mail.gmail.com.


--
Kerline Lorantin, PMP
Associate Director, Enterprise Applications
Strategic Digital Infrastructure
The University of the South
Sewanee, TN 37383
Office: 931.598.1826

Keith Andersen

unread,
Sep 19, 2023, 5:25:56 PMSep 19
to google-apps-sc...@googlegroups.com
Would you consider instead of a.. border changing the color of the cells that are empty to a very light gray? I'm just thinking that doing so might be easier to code.

In any event, I'll get working on a solution.

How many classifications do you have i.e. athletics, admin etc.

Also can you give me the row headers.

Tks
Keith 

Keith Andersen

unread,
Sep 19, 2023, 6:12:27 PMSep 19
to google-apps-sc...@googlegroups.com
A mock sheet would be most helpful.

Donald Tripdarlinq

unread,
Sep 20, 2023, 5:32:12 AMSep 20
to Google Apps Script Community

I see a few issues in your code that need to be addressed. Let's tackle them one by one.

  1. Typo in Setting Table Border: You have a typo when setting the table border. You wrote able instead of table. Change able.setBorderWidth(0); to table.setBorderWidth(0);.

  2. Checking for Unique Values: The logic for checking unique values in columns 1 and 2 is causing problems. You should use a separate loop to build the unique values arrays before adding rows to the table. Here's how you can do it:

  3. Data Analysts, Donald genes from Nigeria

Kerline Lorantin

unread,
Sep 20, 2023, 10:31:34 AMSep 20
to google-apps-sc...@googlegroups.com
Thank you, Keith. I really appreciate your help. I am not tied to the border style. It was more of an attempt to make it easier for the end user to see. So if there is data in the first cell and the fifth cell, but cells two, three and four are empty, I thought that the dotted line (since I cannot add leaders or dotted tab stops) would be helpful.

There are 144 different classifications in the first column. 

There are 158 different classifications in the second column.

I do not have any row headers.



Kerline Lorantin

unread,
Sep 20, 2023, 10:39:19 AMSep 20
to google-apps-sc...@googlegroups.com
Thank you for your time, Donald. It seems that part of your response is missing. Was there something else that you were going to share under checking for unique values?

- Kerline

Keith Andersen

unread,
Sep 20, 2023, 2:24:32 PMSep 20
to google-apps-sc...@googlegroups.com
Kerline,
I have begun a script to address these issues and will be working on it this afternoon. I hope to have something for you tonight.

Cheers,
Keith 

Kerline Lorantin

unread,
Sep 20, 2023, 3:10:13 PMSep 20
to google-apps-sc...@googlegroups.com
Thank you very much for your time and your efforts, Keith. I really appreciate it. 

Donald Tripdarlinq

unread,
Sep 20, 2023, 6:10:14 PMSep 20
to Google Apps Script Community
SURE, first off all, were you able to resolve it?

Keith Andersen

unread,
Sep 21, 2023, 1:46:47 AMSep 21
to google-apps-sc...@googlegroups.com
i believe ive accomplished your goals excel for the dotted bottom border. That one eludes me as yet.

Take a gander and let me know what needs to change.


Cheers
Keith

kelo...@sewanee.edu

unread,
Sep 21, 2023, 5:12:48 PMSep 21
to Google Apps Script Community
@Keith Thank you very much! I haven't had an opportunity to look at your script yet. I plan to do it tomorrow. What do you mean by the following, " alt very least col 1 should be sorted ascending to work properly"? Also, is the thinking that I should convert to pdf from GoogleSheets and omit the GoogleDocs part?

@Donald still working on it

Keith Andersen

unread,
Sep 21, 2023, 5:57:22 PMSep 21
to google-apps-sc...@googlegroups.com
I just did a video and came up with a second more flexible formatting method.
Here is the video with a link to the new sheet in the description.

When you import the initial data, if column 1 is not sorted, you will not get a cascading effect of similar titles under each other.

No,  I wouldn't go right to pdf. to properly format totally with coding - you would need a ton more coding to format and make a pdf. It's possible, but the second method I exampled may be easier and give more artistic control.

Email or check back in here if you have questions. If you need to discuss over phone, private email me and I'll send my number.

Cheers
Keith
heres the sheet for convenience.

kelo...@sewanee.edu

unread,
Sep 22, 2023, 5:30:39 PMSep 22
to Google Apps Script Community
Keith, this is just above and beyond. Thank you! I've learned a lot from this interaction. I will let you know if I have any follow-up questions. 

Keith Andersen

unread,
Sep 22, 2023, 5:32:25 PMSep 22
to google-apps-sc...@googlegroups.com
You're very welcome. My pleasure. Don't hesitate to reach out if you need something changed or clarified.

God bless
Keith 

Reply all
Reply to author
Forward
0 new messages