Search code examples
google-apps-scriptgoogle-docs

How can I create a new Google Doc in the same folder entitled with the text of the current sentence where the cursor is located?


My goal

Let's say I am editing Google Doc #1.

Now let's say I decide, "Hey, I would like to create a new Google Doc entitled, 'I like cheese pizza' in the same folder as Google Doc #1."

Furthermore, let's say I would like the Google Doc named, "I like cheese pizza" to contain a first paragraph which consists of the text, "I like cheese pizza".

How would I accomplish my goal?

I am imagining that while I am editing Google Doc #1, I would type, "I like cheese pizza" and then run an Apps Script.

Possibly helpful examples

As an example, among other things, the following script, which I found at Extending Google Docs, inserts a paragraph that contains the same text as the document's name...

function createDoc() {
  var doc = DocumentApp.create('Sample Document');
  var body = doc.getBody();
  var rowsData = [['Plants', 'Animals'], ['Ficus', 'Goat'], ['Basil', 'Cat'], ['Moss', 'Frog']];
  body.insertParagraph(0, doc.getName())
      .setHeading(DocumentApp.ParagraphHeading.HEADING1);
  table = body.appendTable(rowsData);
  table.getRow(0).editAsText().setBold(true);
}

How can I retrieve the sentence where the cursor currently resides in a Google Doc? apparently accomplishes what it says.

var cursor = DocumentApp.getActiveDocument().getCursor();
var surroundings = cursor.getSurroundingText();
var marker = '\u200B';  // A zero-wdith space character - i.e. a hidden marker.
cursor.insertText(marker);
var surroundingTextStr = surroundings.getText();  // Copy to string
surroundings.replaceText(marker, '');  // Remove the marker from the document.

// Build sentence pattern, allowing for marker.
var pattern = /([A-Z\u200B][^\.!?]*[\.!?])/ig;  
var match;
while ((match = pattern.exec(surroundingTextStr)[0]) != null){
  Logger.log(match);
  if (/\u200B/.test(match)){
    match = match.replace(marker, '');
    Logger.log("We found the sentence containing the cursor!");
    Logger.log(match);
  }
}

Of course the following would create a text file named "'New Text File" with the text, "Hello, world!" That's not what I want. Yet I suppose it still might be helpful. Creates a text file in the current folder with the given name and contents...

// Create a text file with the content "Hello, world!"
DriveApp.getRootFolder().createFile('New Text File', 'Hello, world!');

Solution

  • This should do. It creates a menu from where you can invoke the script. I don't know about the use case but maybe managing new files with sheets is simpler?

    function onOpen(e) {
      DocumentApp.getUi().createMenu('NewDoc')
        .addItem('Create', 'newDocWithSentense')
        .addToUi();
    }
    
    function newDocWithSentense() {
      const doc = DocumentApp.getActiveDocument();
      const cursor = doc.getCursor();
      const surroundings = cursor.getSurroundingText().getText();
    
      const currentFileFolder = DriveApp.getFileById(doc.getId()).getParents().next()
      const newDoc = DocumentApp.create(surroundings).getId()
      DriveApp.getFileById(newDoc).moveTo(currentFileFolder)
    }