3

I have Use Following Code For Insert Text In Google Docs. But Its Not Work. Google Also Not provided any single example in JavaScript for batchupdate. anyone knows about that thing?

  function makeApiCall() {
      var updateObject = {
          documentId: 'My-Document-Id',
          resource: {
              requests: [{
                  "insertText": {
                      "text": "Sameer Bayani",
                      "location": {
                          "index": 25,
                      },
                  },
              }],
          },
      };
      gapi.client.docs.documents.batchUpdate(updateObject, function (e, r) {
          if (e) {
              console.log(e);
          } else {
              console.log(r.data);
          }
      });
  }
1
  • Describe what does not work. Do you see any errors (using F12 tools for example) Commented May 1, 2019 at 17:04

1 Answer 1

2

How about this modification?

Modification points:

  • I think that your request body is correct.
    • But I used 1 as index of location as a test. In this case, the text can be also inserted to new Document. Because I thought that when 25 is used, an error might occur.
  • I think that your script of function (e, r) {if (e) {console.log(e);} else {console.log(r.data);}} is for googleapis of Node.js. So I modified this to Javascript.
    • About Its Not Work., I thought that the reason might be this. Because in your script, no response is returned.

Modified script:

function makeApiCall() {
  var updateObject = {
    documentId: 'My-Document-Id',
    resource: {
      requests: [{
        insertText: {
          text: "Sameer Bayani",
          location: {
            index: 1, // Modified
          },
        },
      }],
    },
  };
  gapi.client.docs.documents.batchUpdate(updateObject)
  .then(function(res) { // Modified
    console.log(res);
  },function(err) {
    console.error(err);
  });
}

Note:

  • This modified script supposes that you have already used Google Docs API by Javascript. When the authorization of your script works, above modified script works. If the errors related to authorization and Docs API occur, please confirm about the script and whether Docs API is enabled.
  • In order to test this script, I used https://www.googleapis.com/auth/documents as the scope.

Reference:

If I misunderstood your question, I apologize.

Sign up to request clarification or add additional context in comments.

1 Comment

@Sameer Bayani Thank you for replying. I'm glad your issue was resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.