简体   繁体   English

如何使用谷歌应用脚本将谷歌文档标题下的特定文本/行导入谷歌表格?

[英]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谢谢

Use getChild() and and push() functions使用 getChild() 和 push() 函数

Setup设置

I used the following as setup:我使用以下设置:

测试

Script脚本

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 将数据附加到电子表格。

Output Output

输出

Update更新

Strictly for this format:严格用于这种格式: 设置 2

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
}

References:参考:

There are a couple of assumptions for this script:这个脚本有几个假设:

  • There is a finite number of header styles you're using (eg 'Header 1' and 'Header 2' in my example below)您正在使用的 header styles 数量有限(例如,下面我的示例中的“标题 1”和“标题 2”)
  • Your questions always contain 'QUESTION:'您的问题始终包含“QUESTION:”
  • There is no other text apart from the headers and the question lines (but if there is, in principle it will be skipped)除了标题和问题行之外没有其他文本(但如果有,原则上将被跳过)

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.

相关问题 如何使用Google Apps脚本将Google文档文本导入电子邮件草稿 - How to Import Google doc text into Email draft using Google Apps Script 如何使用 Google Apps 脚本删除 Google 文档中的选定文本 - How to delete selected text in a Google doc using Google Apps Script 如何使用谷歌应用程序脚本在谷歌文档中的特定文本之后从工作表插入表? - How to insertTable from sheet after specific text in google docs using google apps script? 如何使用应用程序脚本将照片从谷歌表格添加到谷歌文档? - How to add photos to google doc from google sheet using apps script? 如何使用谷歌应用脚本获取谷歌文档中的所有 h1 标题? - How do i get all h1 headings in google doc with google apps script? 使用谷歌应用程序脚本从谷歌文档中提取文本 - Extract Text from google doc using google apps script 使用 Google 表格上的 Google Apps 脚本将多个电子表格导入 1 张表格 - Import Multiple Spreadsheets into 1 Sheet using Google Apps Script on Google Sheets 使用Google表格中的输入来换行(Google Apps脚本) - Wrap text using input from Google Sheet (Google Apps Script) 使用 HTML 和谷歌应用程序脚本突出显示谷歌表格单元格中的文本 - Highlighting text in a google sheet cell using HTML and google apps script 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM