With GAS, how to keep columns width when copy and past TAB from sheet to doc?

Viewed 72

My code below imports to a Gdocument a table (rows and col) selected in Gsheet but my problem is that the columns width are not kept when the tab is copied in the Gdocument so I spend hours to set again the columns width in the Gdocument.

Do you have any suggestion to help my tab to keep its columns width?

Here is my code :

function readFromSpreadsheetAndWriteOnDoc() 
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourcesheet = ss.getSheetByName('Feuille 1');
var selection = ss.getActiveRange().getA1Notation();
var srcData = sourcesheet.getRange(selection).getValues();

var doc = DocumentApp.openById('1a2X5oZhW2aY-p15vMvvPt9wvhm1UVHJU5_3ZJ3-OCBo'
var body = doc.getBody();

body.insertParagraph(3, 'My Title').setHeading(DocumentApp.ParagraphHeading.HEADING1).
setAlignment(DocumentApp.HorizontalAlignment.CENTER);
table = body.appendTable(srcData);

table.getRow(0).editAsText().setBold(true);
}
1 Answers

There is a way to get the width of specific cells using the Sheet API get method (Documentation Here). But you would need to do that for individual cells in each column (like the top cell in each column). Then you can use the setColumnWidth() method (Documentation here) to recreate the right sizes in thew table.

This process would be quite convoluted, I would suggest just using the setColumnWidth() method when you're building the table (table.setColumnWidth(), for example) to do it in a sort of manual way. This would allow you more control on specific cells.

Related