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

时间:2014-10-13 20:59:17

标签: google-apps-script

我有一个Google表单,在提交时,电子表格会运行一个脚本来比较变量并从模板创建一个新文档。我想在脚本中的变量不同时突出显示文档中的文本。我已经发现在执行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);

我有27个目标/密钥匹配变量。有没有办法循环使用,所以我没有为27中的每一个重新创建var targetvar seachResultvar thisElementvar thisElementText

由于

编辑:

我已经发现我很可能需要使用数组遍历变量。这是我更新的代码:

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);

我仍然坚持如何让它只在变量不匹配时突出显示文档中的字段。

1 个答案:

答案 0 :(得分:0)

我想出了答案。新代码:

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);  

由于

相关问题