我设置了这些样式:
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 docTitle = noteBody.appendParagraph(title);
docTitle.setAttributes(titleStyle);
这不会使节标题居中!
var nextHeader = noteBody.appendTable(headerTable);
nextHeader.setAttributes(sectionStyle);
但它确实可以做所有其他事情 - 大小、粗体、下划线等。只是不居中!我可能会错过什么?完整脚本如下
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);
}
从您的显示脚本中,我可以注意到您的实际问题与表格单元格相关。来自
The line where I try to setAttributes is marked off with ////HERE.
,既然如此,下面的修改如何?如果是单元格值,则使用段落。
请按如下方式修改您的显示脚本。
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);
items[i].getTitle()
的单元格值的对齐设置为中心。不幸的是,我不知道你的实际情况。所以,我无法测试你的整个脚本。但是,当我使用您的显示脚本测试一个简单的示例脚本时,我确认修改后的脚本有效。以下脚本是用于测试的简单示例脚本。运行此脚本时,会创建一个新的 Google 文档。打开后可以看到“样本值”的文字居中对齐。
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.setAttributes(sectionStyle);
nextHeader.setBorderWidth(0);
}