设置:
我有一个谷歌表单将数据发送到谷歌表单。Google Sheet本身就进行一些计算。然后,我使用应用程序脚本将计算出的值作为表格发送到谷歌文档中。
问题:没有办法(据我所知),选择";表";变量,并仅将属性应用于列。
我想做的是:类似于table.setColumnAttributes(1,style)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = "DO NOT CHANGE"; // name of sheet to use
var sheet = ss.getSheetByName(sheetName);
//get the last r and column of data
var sheetLR = sheet.getLastRow();
var sheetLC = sheet.getLastColumn();
// define the range containing the data
// this can be changed depending on which columns to embed
var range = sheet.getRange(1,8,12,4);
// Logger.log("DEBUG: the range is "+range.getA1Notation());
// note - this is a change mentioned in
// Embed Google Spreadsheet table Withing Google Document when data contains dates
// when the data includes a data, "getValues()" fails hence
// changing the method to "getDisplayValues()"
var values = range.getDisplayValues();
// get the document details
var docid = "*************DOCID************";
var doc = DocumentApp.openById(docid);
var body = doc.getBody();
var ranges = doc.getNamedRanges('embeddedSheet-range');
if (ranges.length == 0) {
Logger.log("DEBUG: ranges is zero")
var table = body.appendTable(values);
}
else {
Logger.log("DEBUG: ranges is NOT zero")
tableRange = ranges[0];
table = tableRange.getRange().getRangeElements()[0].getElement();
var ind = body.getChildIndex(table);
tableRange.remove();
body.removeChild(table);
table = body.insertTable(ind, values);
table.setColumnWidth(0, 60);
table.setColumnWidth(2, 60);
table.setColumnWidth(3, 60);
var style = {};
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
var test = table.getChild(1);
test.setAttributes(style);
}
var rangeBuilder = doc.newRange();
rangeBuilder.addElement(table);
doc.addNamedRange('embeddedSheet-range', rangeBuilder.build());
}
我没有看到任何默认的方法来设置某个列的属性(颜色、字体大小等)。(您可以使用table.setColumnWidth本机设置列宽,但不能使用其他属性)
下面的代码(来自上面),突出显示了一行,但仍然丢失了如何获得列
var style = {};
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
var test = table.getChild(1);
test.setAttributes(style);
让我们检查一下"文档结构";。我们看到表对象没有直接的列子对象。所以我们应该相应地更新列中每个单元格的样式。例如,第一个表和第4列样式以以下方式修改:
function setTableColProperties() {
const COL_INDEX = 3;
var style = {};
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000cc';
var body = DocumentApp.getActiveDocument().getBody();
var table = body.getTables()[0]; // the table for styling
for (var i = 0; i < table.getNumRows(); i++) {
table.getCell(i, COL_INDEX).setAttributes(style);
}
}
table.getCell()方法用于访问相同的列单元格。