[英]How to import specific text/line under headings in Google doc to Google sheet using google apps script?
I am trying to get specific text/line that is under the heading or subheading using the google apps script.我正在尝试使用谷歌应用程序脚本获取标题或副标题下的特定文本/行。 Here is the screenshot of the Google doc:
这是谷歌文档的截图:
I am looking to export the Question text/line from every heading to google sheets like this:我希望将问题文本/行从每个标题导出到谷歌表格,如下所示:
So far I have been able to get all the headings from the google doc by this code snippet:到目前为止,我已经能够通过这段代码片段从谷歌文档中获取所有标题:
function getHeadings() {
var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
var hdg = [];
for(var i=0; i<pars.length; i++) {
var par = pars[i];
hdg.push(par.getHeading());
}
Logger.log(hdg)
}
But I am unable to get the Question text under all these headings, kindly can you guide me in the right direction?但是我无法在所有这些标题下获得问题文本,请您指导我正确的方向吗? (I apologize for my question formatting, I am a newbie on this website).
(我为我的问题格式道歉,我是这个网站的新手)。 Thank you
谢谢
I used the following as setup:我使用以下设置:
function getHeadings() {
var doc = DocumentApp.getActiveDocument();
var childNum = doc.getBody().getNumChildren();
var ss = SpreadsheetApp.openById("<spreadsheetID>").getSheetByName("Sheet1"); //Change ID of Spreadsheet and Sheet name
var out = [];
for (var i = 0; i < childNum; i += 2) {
out.push([doc.getBody().getChild(i).asText().getText()]);
}
for (var i = 1, j = 0; i < childNum; i += 2, j++) {
out[j].push(doc.getBody().getChild(i).asText().getText().replace(/Question(.*)- /gm, ""));
}
ss.getRange(ss.getLastRow() + 1, 1, childNum / 2, 2).setValues(out); //edit range to proper coordinates
}
The script uses the getChild()
function to get the texts in the document.该脚本使用
getChild()
function 来获取文档中的文本。 Afterwards, the getNumChildren()
will get the number of children to be used in the two separate for loops which will segregate the heading and the question texts.之后,
getNumChildren()
将获得要在两个单独的 for 循环中使用的子项的数量,这将分隔标题和问题文本。 After the texts are identified, they are separated, paired, and then pushed (using the push()
function) into arrays.识别文本后,将它们分离、配对,然后推送(使用
push()
函数)到 arrays。 Once the arrays are formed, the data will be appended to the spreadsheet using the setValues()
function.形成 arrays 后,将使用
setValues()
function 将数据附加到电子表格。
Strictly for this format:严格用于这种格式:
You may use the following script as basis for your code:您可以使用以下脚本作为代码的基础:
function getHeadings() {
var doc = DocumentApp.getActiveDocument();
var childNum = doc.getBody().getNumChildren();
var ss = SpreadsheetApp.openById("<SpreadsheetID>").getSheetByName("Sheet1"); //Change ID of Spreadsheet and Sheet name
var header = [];
var question = [];
var out = [];
for (var i = 0; i < childNum; i++) {
if (doc.getBody().getChild(i).asText().getText().match(/Header(.*)/gm)){
header.push(doc.getBody().getChild(i).asText().getText());
}
else if (doc.getBody().getChild(i).asText().getText().match(/Question(.*)/gm)){
question.push(doc.getBody().getChild(i).asText().getText());
}
}
for (j=0; j<header.length; j++) {
out.push([header[j]]);
out[j].push(question[j].replace(/Question(.*)- /gm, ""));
}
ss.getRange(ss.getLastRow() + 1, 1, header.length, 2).setValues(out); //edit range to proper coordinates
}
There are a couple of assumptions for this script:这个脚本有几个假设:
In that case, the below code will work:在这种情况下,以下代码将起作用:
function getHeadings() {
var pars = DocumentApp.getActiveDocument().getBody().getParagraphs();
var currentHdg = "";
var questions = [];
for(var i=0; i<pars.length; i++) {
var par = pars[i];
var text = par.getText();
var hdg = par.getHeading();
if (hdg.toString().indexOf("HEADING") > -1){
currentHdg = text;
}
else if(text.indexOf("QUESTION:") > -1){
questions.push([currentHdg,text.replace("QUESTION:","").trim()]);
}
}
Logger.log(questions);
}
You can then format questions
into the table output format you need.然后您可以将
questions
格式化成您需要的表格 output 格式。
Edit: I have updated my answer to cover all heading types.编辑:我已经更新了我的答案以涵盖所有标题类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.