简体   繁体   English

如果变量不同,Google脚本突出显示文本

[英]Google Script Highlight Text If Variables Differ

I have a Google Form and on submission, the spreadsheet runs a script to compare variables and create a new document from a template. 我有一个Google表单,提交后,电子表格会运行一个脚本来比较变量并通过模板创建一个新文档。 I am wanting to also highlight the text in the document when the variables differ in the script. 当脚本中的变量不同时,我还要突出显示文档中的文本。 I've figured out that I need to format the text in the document first before I do a replaceText(). 我发现我需要先对文档中的文本进行格式化,然后再执行replaceText()。

var matching = e.values[29]; //column in spreadsheet
var keymatching = "2b, 6a, 7a, 8b";

// Get document template, copy it as a new temp doc to folder, and save Doc's id
var targetFolder = DocsList.getFolderById("0B4bnlgTJUvsgeV9XelN4a243Y1k");
var file = DocsList.getFileById(docTemplate)
.makeCopy(docName+' ~ '+first_name+' '+last_name);
var copyId = file.getId();
file.addToFolder(targetFolder);

// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document's body section
var copyBody = copyDoc.getActiveSection();

var background = background || '#F3E2A9';  // default color is light orangish.
var target = 'keyMatching' //text in document
var bodyElement = copyDoc.getBody()
var searchResult = bodyElement.findText(target);
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText(); 

if (matching == keymatching) {} else    {thisElementText.setBackgroundColor(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),background)};

// Replace place holder keys, in our google doc template
copyBody.replaceText('keyMatching', matching);

I have 27 targets/key matching variables. 我有27个目标/关键匹配变量。 Is there a way to loop this so I'm not recreating var target , var seachResult , var thisElement and var thisElementText for each of the 27? 有没有一种方法可以循环播放,这样我就不会为27个对象中的每一个重新创建var targetvar seachResultvar thisElementvar thisElementText

Thanks 谢谢

Edit: 编辑:

I have figured out that I most likely need to loop through the variables using an array. 我发现我很可能需要使用数组来遍历变量。 Here is my updated code: 这是我更新的代码:

var matching = e.values[29]; //column in spreadsheet
var keymatching = "2b, 6a, 7a, 8b";

// Get document template, copy it as a new temp doc to folder, and save Doc's id
var targetFolder = DocsList.getFolderById("0B4bnlgTJUvsgeV9XelN4a243Y1k");
var file = DocsList.getFileById(docTemplate)
.makeCopy(docName+' ~ '+first_name+' '+last_name);
var copyId = file.getId();
file.addToFolder(targetFolder);

// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document's body section
var copyBody = copyDoc.getActiveSection();

var background = background || '#F3E2A9';  // default color is light orangish.
var target = ["keyMatching", "keyCount1", "keyCount2"]; //text in document, made an array
var bodyElement = copyDoc.getBody()

for (i in target) {
var searchResult = bodyElement.findText(target[i]);
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText(); 

if (matching == keymatching) {} else    {thisElementText.setBackgroundColor(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),background)};
}  
// Replace place holder keys, in our google doc template
copyBody.replaceText('keyMatching', matching);

I'm still stuck on how to get it to only highlight the field in the document when the variables do not match. 我仍然停留在如何使它仅在变量不匹配时突出显示文档中的字段的问题。

I figured out the answer. 我想出了答案。 New code: 新代码:

var matching = e.values[29]; //column in spreadsheet
var keymatching = "2b, 6a, 7a, 8b";

// Get document template, copy it as a new temp doc to folder, and save Doc's id
var targetFolder = DocsList.getFolderById("0B4bnlgTJUvsgeV9XelN4a243Y1k");
var file = DocsList.getFileById(docTemplate)
.makeCopy(docName+' ~ '+first_name+' '+last_name);
var copyId = file.getId();
file.addToFolder(targetFolder);

// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document's body section
var copyBody = copyDoc.getActiveSection();

var background = background || '#F3E2A9';  // default color is light orangish.
var target, targets = ['keyMatching', 'keyCount1', 'keyCount2']; //text in document, made an  array
var bodyElement = copyDoc.getBody()

for (var i=0, iLen=targets.length; i<iLen; i++)
{ target = targets[i]
var searchResult = bodyElement.findText(target);
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText(); 

if (target == 'keyMatching')  { 
 if (matching == keymatching) {} else  {thisElementText.setBackgroundColor(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),background)};
 }
  if (target == 'keyCount1') {
   if (count1 == keycount1) {} else {thisElementText.setBackgroundColor(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),background)};
  }
   if (target == 'keyCount2') {
    if (count2 == keycount2) {} else {thisElementText.setBackgroundColor(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),background)};
    } 
}
// Replace place holder keys, in our google doc template
copyBody.replaceText('keyMatching', matching);  

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM