简体   繁体   中英

Get text from a Google Docs

I am quite new the using Google Apps Script, but not to JavaScript. I was wondering if there was a way that I could retrieve some text from a Google Docs, and send it in an email, something like this. This is not the correct syntax, but bear with me.

function createAndSendDocument() {
    var email = "email";
    var subject = "Subject";
    var body = 
        GmailApp.sendEmail(email, subject, body);
}

Is there a way that I could do something like this,

var randnum = random(1,4)
getText(documentname, text from document)

Text on the document would look something like this.

  1. Hello

  2. Test

  3. How are you?

  4. Watermelon, Where getting the text from the document is based on randnum: example: randnum = 1 so email body would be "Hello"

Sorry for all the jumping around in the question, but hope you can answer this. Comment if you need any clarification.

I believe I understand what you are looking for.

The tricky part is understanding how you have formatted the list in the Google Doc. A list can be formatted as an actual indented list where the list items are automatically incremented as you enter a new line or a list can just be a hardcoded list of entries where nothing is automatically indented and the list items are manually incremented. See the two examples in this document . Accessing the lines is different depending on how the list is built. If you truly only care about finding a particular numbered child, I would probably suggest formatting it as an actual list. If you think you might require more advanced searching, I would format it as a hardcoded list, because then you can use some regular expressions to find the relevant child.

Now to actually grabbing the text, this is what I came up with (please note that this is written in the new-ish V8 engine, so you won't be able to use this in the legacy script editor):

const getTextFromDocument = (documentID, randomNumber) => {
  const doc = DocumentApp.openById(documentID);
  const body = doc.getBody();

  // get text from non-list elements
  const find = body.findText('^' + randomNumber);

  if (find) {
    return find.getElement().asText().getText();
  }

  // get text from list elements
  const listItems = body.getListItems();
  for (let lix = 0; lix < listItems.length; lix++) {
    const listItem = listItems[lix];
    if (lix + 1 === randomNumber) {
      return listItem.getText();
    }
  }
}

If the script finds the information in a hardcoded list, the first chunk runs, otherwise it'll proceed into the next chunk.

Depending on how the text is formatted, the returned text is slightly different. If the text is formatted as an actual list, the text looks like this:

Watermelon!

(Without the number.) Whereas if it's a hardcoded listed, the returned text looks like this:

4. Watermelon!

(With the preceding number.)

With that, sending the email will look something like this:

const sendEmail = () => {
  const documentID = '1DljXJtDdZLKr_TcZKuFESo4VrgPEpwXLI3bM3RA0Kjk';
  const randomNumber = Math.floor(Math.random() * 4) + 1;
  const text = getTextFromDocument(documentID, randomNumber);

  console.log(randomNumber);
  console.log(text);

  // MailApp.sendEmail({
  //   to: 'someone@gmail.com',
  //   subject: 'Subject Line',
  //   body: text
  // });
}

I opted for MailApp as opposed to GmailApp, since MailApp is meant for sending emails exclusively whereas GmailApp also offers methods for managing Gmail. You can test it out over here if you want . If you want to take advantage of more email styling, I recommend checking out the guides over here . And if you want to read more about DocumentApp, you can check that out here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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