我正在创建一个Google Apps脚本,用于创建模块化文档。它可以通过Google电子表格上的几个标签进行控制。基本上,它根据各个工作表中的条目将小型源文档合并为较大文档。工作表Groups
控制添加主要部分。工作表Tables
控制向主要部分之一添加子部分。我的主要部分工作正常,但控制子部分的功能无法在文档中正确找到子部分的标题。它会在搜索到的文本出现之前找到大约20个段落的上一部分的段落索引。问题似乎出在线路上
var foundTag = body.findText(sectionToUpdate);
它没有返回文本在文档中的正确位置。
当我登录sectionToUpdate
时,这是正确的部分标题。如果我从日志中复制文本,则手动搜索该文档即可正确找到它。当我记录通过搜索找到的paragraphIndex
中的文本时,它将返回值“ 01”,这是上一节中的表项。
function updateTablesSection(groupName) //for updating table insertion instructions
{
var app = SpreadsheetApp.getActiveSpreadsheet();
var groupsSheet = app.getSheetByName("Groups");
var tablesSheet = app.getSheetByName("Tables");
var groupDocumentsSheet = app.getSheetByName("Group Documents");
var sectionToUpdate = "Adding a user to ";
sectionToUpdate = sectionToUpdate + tablesSheet.getRange(5,1).getValue();
Logger.log(sectionToUpdate);
var destinationDoc = getDoc(groupDocumentsSheet, groupName);
var body = destinationDoc.getBody();
var foundTag = body.findText(sectionToUpdate);
if (foundTag != null) //if the section exists in the destination doc
{
var tagElement = foundTag.getElement();
var parent = tagElement.getParent();
var paragraphIndex = parent.getParent().getChildIndex(parent); //gets the section to update's paragraph index. This marks the header of the section.
var paragraphArray = body.getParagraphs();
Logger.log(tagElement);
Logger.log(parent);
Logger.log(paragraphIndex);
Logger.log(tagElement);
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
paragraphIndex++;
Logger.log(paragraphArray[paragraphIndex].getText());
Logger.log(paragraphIndex);
}
}//end function updateTablesSection
function getDoc(sheet, name) //This function takes in a sheet and a string. returns a doc. name must be in a list in column one of the sheet. column 2 contains doc IDs
{
var k = 2;
var ID = "";
var fileIterator = "";
var url = "";
while(k <= sheet.getLastRow())
{
if (name == sheet.getRange(k, 1).getValue())
{
ID = sheet.getRange(k, 2).getValue();
fileIterator = DriveApp.getFileById(ID);
url = fileIterator.getUrl();
return (DocumentApp.openByUrl(url));
}
k++;
}
} //end function getDoc
这是记录的输出
[19-06-15 15:50:44:930 PDT] Adding a user to user_permit
[19-06-15 15:50:45:991 PDT] Text
[19-06-15 15:50:45:991 PDT] Paragraph
[19-06-15 15:50:45:991 PDT] 100.0 <-- it returns this index number
[19-06-15 15:50:45:992 PDT] Text
[19-06-15 15:50:45:992 PDT]
[19-06-15 15:50:45:992 PDT] 01
[19-06-15 15:50:45:993 PDT] ALL
[19-06-15 15:50:45:993 PDT] -1
[19-06-15 15:50:45:994 PDT] <site number>
[19-06-15 15:50:45:994 PDT] LASTNAME.FIRSTNAME
[19-06-15 15:50:45:994 PDT] NOTE: There should be no pipes at the beginning or end of the Contact field
[19-06-15 15:50:45:995 PDT] Press Number Pad Enter to append and Number Pad * to save the row
[19-06-15 15:50:45:995 PDT] Fill in your template with the database table
[19-06-15 15:50:45:996 PDT] Adding a user to user_permit
[19-06-15 15:50:45:996 PDT] 109.0 <--this index number is where the value actually is