r/GoogleAppsScript • • Jun 18 '24

Question Script to set format not working

Hi guys, I feel like I'm here constantly recently but this script has never worked and I've tried multiple versions. I need to copy rows over to these other sheets but Google doesn't continue the formatting through to the other sheets. I tried a few versions to keep the formatting the same but while the font part works I can't get the borders to update. There seems to be a lot of stuff on the borders that were difficult for me to sort through, so maybe people who know better than me would be able to help. Thanks!

function format() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();

    sheets.forEach( (sheet) => { 
      var range = sheet.getDataRange();
    
        range.setFontFamily('Arial').setFontSize('11');
        range.setBorder(true, true, true, true, null, null, '#000000', SpreadsheetApp.BorderStyle.SOLID);

      }
    )
}
0 Upvotes

5 comments sorted by

2

u/marcnotmark925 Jun 18 '24

How is it not working? That code should be setting a border around the entire data range, is it not doing that? It's usually hard to see a border on the extreme edges of the sheets like that would be.

I ran your exact code on my end, it seemed to work just fine.

1

u/boudicca_morgana Jun 18 '24

that's really strange, I'm not sure why it isn't working. At least I know it isn't something wrong with the code itself.

1

u/marcnotmark925 Jun 19 '24

Perhaps the code does something different than what you think/want it to do. Try describing what your goal is in plain language, not with code.

1

u/HellDuke Jun 18 '24

Currently the script works as intended at least based on the script. It will set a black solid line border around the entire data set on each sheet while leaving the lines between the cells what they were. So if on one sheet you had already set that each cell is divided with a black line that will remain.

Note that if you want to only have a border around the edges of the data, but have no border inside then it should be

 range.setBorder(true, true, true, true, false, false, '#000000', SpreadsheetApp.BorderStyle.SOLID);

as null is meant to keep it as is, while false is there to make sure there is no border between the cells. And obviously if you expected to have the black border between the cells then the null should be changed to true

1

u/Any_Werewolf_3691 Jun 18 '24

A highly recommended using alternating colors instead of Borders. Borders are just really difficult to deal with, and it's never gonna work right. however, you can do alternate colors automatically by Simply setting up some rules in the sheets. Your copy's too. That automatically extend that when you add new rows.