I have these styles set up:
var titleStyle = {};
titleStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
titleStyle[DocumentApp.Attribute.FONT_SIZE] = 20;
titleStyle[DocumentApp.Attribute.BOLD] = true;
titleStyle[DocumentApp.Attribute.UNDERLINE] = true;
titleStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var sectionStyle = {};
sectionStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
sectionStyle[DocumentApp.Attribute.FONT_SIZE] = 18;
sectionStyle[DocumentApp.Attribute.BOLD] = true;
sectionStyle[DocumentApp.Attribute.UNDERLINE] = true;
sectionStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
This centers the title:
var docTitle = noteBody.appendParagraph(title);
docTitle.setAttributes(titleStyle);
This does not center the section header!
var nextHeader = noteBody.appendTable(headerTable);
nextHeader.setAttributes(sectionStyle);
But it DOES do all the other things - size, bold, underline, etc. Just not center!! What could I be missing? Full script below
var date = new Date();
var zone = Session.getScriptTimeZone();
var formattedDate = Utilities.formatDate(date, zone, 'MM/dd/yy hh:mm a');
var docName = "title here - " + formattedDate;
var doc = DocumentApp.create(docName);
var docFiles = DriveApp.getFilesByName(docName);
var docFile = docFiles.next();
var noteBody = doc.getBody();
var document = [];
/** styles */
var titleStyle = {};
titleStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
titleStyle[DocumentApp.Attribute.FONT_SIZE] = 20;
titleStyle[DocumentApp.Attribute.BOLD] = true;
titleStyle[DocumentApp.Attribute.UNDERLINE] = true;
titleStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var sectionStyle = {};
sectionStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
sectionStyle[DocumentApp.Attribute.FONT_SIZE] = 18;
sectionStyle[DocumentApp.Attribute.BOLD] = true;
sectionStyle[DocumentApp.Attribute.UNDERLINE] = true;
sectionStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var headerStyle = {};
headerStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
headerStyle[DocumentApp.Attribute.FONT_SIZE] = 16;
headerStyle[DocumentApp.Attribute.BOLD] = true;
headerStyle[DocumentApp.Attribute.UNDERLINE] = false;
var gridStyle = {};
gridStyle[DocumentApp.Attribute.FONT_SIZE] = 13;
gridStyle[DocumentApp.Attribute.BOLD] = false;
gridStyle[DocumentApp.Attribute.UNDERLINE] = true;
var tableStyle = {};
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 12;
tableStyle[DocumentApp.Attribute.BOLD] = false;
tableStyle[DocumentApp.Attribute.UNDERLINE] = false;
function autoPDF() {
//find or create folder
var docFolders = DriveApp.getFoldersByName("AutoPDF");
if (docFolders.hasNext()){var docFolder = docFolders.next()}
else {var docFolder = DriveApp.createFolder("AutoPDF")};
docFile.moveTo(docFolder);
var form = FormApp.getActiveForm();
var title = form.getTitle();
var items = form.getItems();
var responseSet = form.getResponses();
var lastResponse = responseSet.length - 1;
var itemResponse = responseSet[lastResponse].getItemResponses(); //this is the set of responses for the most recent submission
var table = [];
title = title + '\n';
var docTitle = noteBody.appendParagraph(title);
docTitle.setAttributes(titleStyle);
var j = 0;
for (i=0; i<items.length; i++){
var itemType = items[i].getType();
var itemTitle = items[i].getTitle();
//question items
if (itemType != 'IMAGE' &&
itemType != 'PAGE_BREAK' &&
itemType != 'SECTION_HEADER' &&
itemType != 'VIDEO' )
{
//additional step needed for multiple answer options - turn array into string
if (itemType == 'CHECKBOX_GRID'){
table.push(['\n' + itemTitle.toUpperCase()]); //add grid item title
var cgItem = items[i].asCheckboxGridItem();
var answerRow = [];
var row = cgItem.getRows();
var numRows = cgItem.getRows().length;
var cgResponse = itemResponse[j].getResponse();
for(n=0; n<numRows; n++){
var rowTitle = " " + row[n].toString();
if (cgResponse[n] == null) {cgResponse[n] = "[no answer]";}
var answers = cgResponse[n].toString();
answerRow = [rowTitle, answers];
table.push(answerRow)
}
}
else if (itemType == 'GRID') {
table.push(['\n' + itemTitle.toUpperCase()]); //add grid item title
var gItem = items[i].asGridItem();
var answerRow = [];
var row = gItem.getRows();
var numRows = gItem.getRows().length;
var gResponse = itemResponse[j].getResponse();
for(n=0; n<numRows; n++){
var rowTitle = " " + row[n].toString();
if (gResponse[n] == null) {gResponse[n] = "[no answer]";}
var answers = gResponse[n].toString();
answerRow = [rowTitle, answers];
table.push(answerRow)
}
}
else {
var tableRow = [itemTitle, itemResponse[j].getResponse()]; //itemResponse[j] is the response to question #j (from the most recent answer set)
//turning response from array into string?
var responseString = "";
for (m=1; m<tableRow.length; m++){
responseString = responseString + tableRow[m];
if (m<tableRow.length-1) {responseString = responseString + ", ";} //if it's the last one don't add this
}
tableRow = [itemTitle, responseString];
table.push(tableRow);
}
j++;
}
//layout items
else if (itemType == 'SECTION_HEADER' || itemType == 'PAGE_BREAK'){
addToDoc(table);
var headerTable = [];
headerTable.push([items[i].getTitle()]);
var nextHeader = noteBody.appendTable(headerTable);
nextHeader.setAttributes(sectionStyle);
//table style
nextHeader.setAttributes(sectionStyle); /////HERE
nextHeader.setBorderWidth(0);
if (i<items.length-1) {table = []}; //clear for a restart if there are more items
}
}
addToDoc(table); //add last table - doesn't end with layout item
/** create PDF from doc and move it to new folder */
var pdfBlob = docFile.getBlob().getAs('application/pdf');
var newPDF = docFolder.createFile(pdfBlob);
newPDF.setName(title + " - " + formattedDate);
docFile.setTrashed(true);
}
function addToDoc(table){
var nextTable = noteBody.appendTable(table);
nextTable.setAttributes(tableStyle);
nextTable.setBorderWidth(0);
nextTable.setColumnWidth(0, 200);
nextTable.setColumnWidth(1, 300);
}
From your showing script, I could notice that your actual question is related to the table cell. From The line where I try to setAttributes is marked off with ////HERE.
, in this case, how about the following modification? In the case of the cell value, the paragraph is used.
Please modify your showing script as follows.
var headerTable = [];
headerTable.push([items[i].getTitle()]);
var nextHeader = noteBody.appendTable(headerTable);
nextHeader.setAttributes(sectionStyle);
var headerTable = [];
headerTable.push([items[i].getTitle()]);
var nextHeader = noteBody.appendTable(headerTable);
// Added
nextHeader.getCell(0, 0).getChild(0).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.CENTER);
nextHeader.setAttributes(sectionStyle);
or
var headerTable = [];
headerTable.push([items[i].getTitle()]);
var nextHeader = noteBody.appendTable(headerTable);
// Modified
// nextHeader.setAttributes(sectionStyle);
nextHeader.getCell(0, 0).getChild(0).asParagraph().setAttributes(sectionStyle);
items[i].getTitle()
is set as the center.In this sample, it supposes that your showing script works fine. Please be careful about this.
Unfortunately, I do not know your actual situation. So, I cannot test your whole script. But, when I tested a simple sample script using your showing script, I confirmed that the modified script worked. The following script is a simple sample script for testing. When this script is run, a new Google Document is created. When you open it, you can see that the text of "sample value" is aligned as the center.
function sample() {
const noteBody = DocumentApp.create("sample").getBody();
// This is from your showing script.
var sectionStyle = {};
sectionStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
sectionStyle[DocumentApp.Attribute.FONT_SIZE] = 18;
sectionStyle[DocumentApp.Attribute.BOLD] = true;
sectionStyle[DocumentApp.Attribute.UNDERLINE] = true;
sectionStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
// This is from your showing script.
var tableStyle = {};
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 12;
tableStyle[DocumentApp.Attribute.BOLD] = false;
tableStyle[DocumentApp.Attribute.UNDERLINE] = false;
// Below script is from your showing script.
var headerTable = [];
headerTable.push(["sample value"]); // This is a sample value.
var nextHeader = noteBody.appendTable(headerTable);
nextHeader.getCell(0, 0).getChild(0).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.CENTER);
nextHeader.setAttributes(sectionStyle);
nextHeader.setBorderWidth(0);
}
or
function sample() {
const noteBody = DocumentApp.create("sample").getBody();
// This is from your showing script.
var sectionStyle = {};
sectionStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
sectionStyle[DocumentApp.Attribute.FONT_SIZE] = 18;
sectionStyle[DocumentApp.Attribute.BOLD] = true;
sectionStyle[DocumentApp.Attribute.UNDERLINE] = true;
sectionStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
// This is from your showing script.
var tableStyle = {};
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 12;
tableStyle[DocumentApp.Attribute.BOLD] = false;
tableStyle[DocumentApp.Attribute.UNDERLINE] = false;
// Below script is from your showing script.
var headerTable = [];
headerTable.push(["sample value"]); // This is a sample value.
var nextHeader = noteBody.appendTable(headerTable);
nextHeader.getCell(0, 0).getChild(0).asParagraph().setAttributes(sectionStyle);
nextHeader.setBorderWidth(0);
}