简体   繁体   English

使用Apps脚本从Google电子表格添加到Google文档

[英]Adding to a Google Doc from a Google Spreadsheet Using Apps Script

I'm new to using the DocumentApp in Google Apps, so looking for some help! 我是在Google Apps中使用DocumentApp的新手,所以寻求帮助!

I'm trying to create an FAQ (in a Google Doc) automatically from a spreadsheet. 我正在尝试通过电子表格自动创建一个常见问题解答(在Google文档中)。 If certain conditions are met in the spreadsheet row, I want the script to find the category of the question in the document and insert a new question and response underneath it (pushing anything already there back). 如果在电子表格行中满足某些条件,我希望脚本在文档中找到问题的类别,然后在其下插入一个新的问题和答案(将已经存在的内容推回去)。

So I'd have a document that looks like this: 所以我会有一个看起来像这样的文档:

https://docs.google.com/document/d/1fjb3RO6hUY6n7x0bu6WvtcleMWRNC8VQ9U82hXiGqcY/edit?usp=sharing https://docs.google.com/document/d/1fjb3RO6hUY6n7x0bu6WvtcleMWRNC8VQ9U82hXiGqcY/edit?usp=sharing

And a spreadsheet that looks like this: 和一个电子表格,看起来像这样:

https://docs.google.com/spreadsheets/d/1fb3ceqP6142_C7QQ1PfkWtVNswOyzOxkWCofvauf4Ps/edit?usp=sharing https://docs.google.com/spreadsheets/d/1fb3ceqP6142_C7QQ1PfkWtVNswOyzOxkWCofvauf4Ps/edit?usp=sharing

And this is the code I'm trying to use. 这就是我要使用的代码。 I'm getting a ton of errors- mainly because I don't have a good grasp on what the different elements involved are. 我收到大量错误-主要是因为我对所涉及的不同元素没有很好的了解。 Can anyone point me in the right direction? 谁能指出我正确的方向?

function insertnewquestion() {

//(works fine)Get active document and related spreadsheet  
var doc = DocumentApp.getActiveDocument().getBody();
var ss = SpreadsheetApp.openById("xxxxxxx");
var sheet = ss.getSheetByName("xxxxx");

//(works fine)Go through the various rows in the spreadsheet up to the last row  
for (var row = 2; row <= sheet.getLastRow(); ++row) {
var status = sheet.getRange(row,4).getValue();
var question = sheet.getRange(row,1).getValue();
var response = sheet.getRange(row,2).getValue();
var category = sheet.getRange(row,3).getValue();
var date = sheet.getRange(row,5).getValue();

//(works fine)find rows with blank status and a response filled in    
if (status !== "Entered" && response !== "") {

//(errors! this is where i need help) find the pertinent header  

var categoryposition = body.findText(category);  //looking for the category header- new text should be added after this, the idea is that what was already under this header will move down
var questionheader = categoryposition.appendText(question);  //trying here to add in question text after where the category was found
questionheader.setHeading(DocumentApp.ParagraphHeading.HEADING3); //set question text as heading 3 (so it shows up on table of contents)
questionheader.appendText("\r"+"\r"+response+"\r\r"); //add line breaks and then the response text in normal font, then more line breaks to put space between new stuff and old stuff

//(works fine)Mark in the spreadsheet that it was entered in FAQ and the date so that it isn't double entered next time the script runs

sheet.getRange(row,4).setValue("Entered");
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = (month + "/" + day + "/" + year) 

sheet.getRange(row,5).setValue(date);

}
  else {continue}


}

//(need help!) Refresh table of contents to include new questions

//no idea how to do this!

}

In the code you are referring to body.findText(category); 在代码中,您指的是body.findText(category); where it should be doc.findText(category); 应该在doc.findText(category); . Also for the line: 同样针对该行:

var categoryposition = body.findText(category);

it returns the RangeElement — a search result indicating the position of the search text, or null if there is no match 它返回RangeElement — a search result indicating the position of the search text, or null if there is no match则返回RangeElement — a search result indicating the position of the search text, or null if there is no match

Before adding any lines of code you have to check for null value in categoryposition. 在添加任何代码行之前,您必须检查categoryposition中的空值。

The text class has a method to insert text in particular offset value to particular String value gives as shown here . 文本类有插入给出如图特定偏移值与特定字符串值文本的方法在这里

Hope that is helpful. 希望对您有所帮助。

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

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