Phillip Armstrong
Phillip Armstrong

Reputation: 25

parse google document for text and copy result to sheet

I wish to parse a series of documents in a Google Drive folder using regular expressions.

The documents contain equipment model and serial numbers. I wish to then copy the results to a google sheet row by row. I have managed a similar task with emails successfully but to no avail with google docs.

Can anyone offer some guidance. I have tested the regular expressions in the 'find and replace' menu in google docs and they work fine. The following is simply an attempt to see if I can capture some data and write it to a cell in the active sheet.

function write() {
var ss= SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var doc = 
DocumentApp.openById('1ZNqJjSJo1wkD3eaCRTY64g98hYEY77D4MDU6XpvA4MI');
var body = doc.getBody();
var text = body.findText('(\W|^)GSS\d{2}H(\W|$)')
  ss.getRange(1,1).setValue(text);

}

Upvotes: 1

Views: 3180

Answers (1)

Tanaike
Tanaike

Reputation: 201613

You want to retrieve all values matched by (\W|^)GSS\d{2}H(\W|$) in the document, and put the result to spreadsheet with row by row. If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.

Modification points :

  • Retrieve text from document.
  • Retrieve all matched values using the regex.
  • Put the result to spreadsheet.

Modified script :

function write() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var doc = DocumentApp.openById('1ZNqJjSJo1wkD3eaCRTY64g98hYEY77D4MDU6XpvA4MI');
  var body = doc.getBody();

  // Modified script
  var text = doc.getBody().getText();
  var result = [];
  var r = /(\W|^)GSS\d{2}H(\W|$)/g;
  while ((res = r.exec(text)) !== null) { // or while (res = r.exec(text)) {
    result.push([res[0]]);
  }
  ss.getRange(ss.getLastRow() + 1, 1, result.length, 1).setValues(result);
}

If this was not what you want, I'm sorry. At that time, could you please provide the sample input and output you need? I would like to modify my answer.

Upvotes: 2

Related Questions