Google 文档 <attribute>.HORIZONTAL_ALIGNMENT == "Center" 返回 false,但说 "Center"
Google Docs <attribute>.HORIZONTAL_ALIGNMENT == "Center" returning false, but saying "Center"
我有一个顶部有 Title 元素的文档,我正在尝试检查标题是否居中。我已经设置了以下内容:
const body = DocumentApp.getActiveDocument().getBody();
let title = body.getParagraphs()[0];
let att = title.getAttributes();
}
当我添加 Logger.log(att.HORIZONTAL_ALIGNMENT);
时,它 returns “居中”,但是当我尝试用 Logger.log(att.HORIZONTAL_ALIGNMENT == "Center");
对其进行检查时,它 returns 错误。其他属性检查正确,如字体大小、字体、粗体等
那个属性是一个ENUM对象,所以需要和DocumentApp.HorizontalAlignment
的相关属性进行比较:
function myFunction() {
const body = DocumentApp.getActiveDocument().getBody();
let title = body.getParagraphs()[0];
let att = title.getAttributes();
Logger.log(att.HORIZONTAL_ALIGNMENT);
Logger.log(att.HORIZONTAL_ALIGNMENT === DocumentApp.HorizontalAlignment.CENTER);
}
请注意使用 ===
而不是 ==
。请参阅 here 了解为什么首选它的背景。
补充说明:
当您将 att.HORIZONTAL_ALIGNMENT
的值记录到控制台时,会导致打印 ENUM 的文本表示,如您所见。
但是当您将相同的 att.HORIZONTAL_ALIGNMENT
放在求值中时,它仍然是一个 ENUM。它在评估发生时尚未转换为文本 - 因此 ENUM 对象不等于“中心”文本。它们是两个不同的东西。
也许这会有所帮助:
文档只有一段
function lookingatattributes() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const style1 = {};
style1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.CENTER;
let n = body.getNumChildren();
for(let i = 0; i<n;i++) {
let c = body.getChild(i).asParagraph();
c.setAttributes(style1);
Logger.log('Child: %s %s %s',i,c.getAttributes()['CENTER'],c.getAttributes()[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]);
}
}
Execution log
2:36:27 PM Notice Execution started
2:36:28 PM Info Child: 0.0 null Center
2:36:27 PM Notice Execution completed
这不是 属性“CENTER”
我有一个顶部有 Title 元素的文档,我正在尝试检查标题是否居中。我已经设置了以下内容:
const body = DocumentApp.getActiveDocument().getBody();
let title = body.getParagraphs()[0];
let att = title.getAttributes();
}
当我添加 Logger.log(att.HORIZONTAL_ALIGNMENT);
时,它 returns “居中”,但是当我尝试用 Logger.log(att.HORIZONTAL_ALIGNMENT == "Center");
对其进行检查时,它 returns 错误。其他属性检查正确,如字体大小、字体、粗体等
那个属性是一个ENUM对象,所以需要和DocumentApp.HorizontalAlignment
的相关属性进行比较:
function myFunction() {
const body = DocumentApp.getActiveDocument().getBody();
let title = body.getParagraphs()[0];
let att = title.getAttributes();
Logger.log(att.HORIZONTAL_ALIGNMENT);
Logger.log(att.HORIZONTAL_ALIGNMENT === DocumentApp.HorizontalAlignment.CENTER);
}
请注意使用 ===
而不是 ==
。请参阅 here 了解为什么首选它的背景。
补充说明:
当您将 att.HORIZONTAL_ALIGNMENT
的值记录到控制台时,会导致打印 ENUM 的文本表示,如您所见。
但是当您将相同的 att.HORIZONTAL_ALIGNMENT
放在求值中时,它仍然是一个 ENUM。它在评估发生时尚未转换为文本 - 因此 ENUM 对象不等于“中心”文本。它们是两个不同的东西。
也许这会有所帮助:
文档只有一段
function lookingatattributes() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const style1 = {};
style1[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]=DocumentApp.HorizontalAlignment.CENTER;
let n = body.getNumChildren();
for(let i = 0; i<n;i++) {
let c = body.getChild(i).asParagraph();
c.setAttributes(style1);
Logger.log('Child: %s %s %s',i,c.getAttributes()['CENTER'],c.getAttributes()[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT]);
}
}
Execution log
2:36:27 PM Notice Execution started
2:36:28 PM Info Child: 0.0 null Center
2:36:27 PM Notice Execution completed
这不是 属性“CENTER”