0

I have a google docs template and I would like to replace some strings from the template with external data, preferably via cURL as the script that should do the changes is a bash script.

I have the search and replace in google app script done like the one below but I don't know how to call the script and replace with some external data instead.

function myFunction() {

var doc = DocumentApp.openById("DOCUMENT_ID");   

  var body = DocumentApp.getActiveDocument().getBody();
  var client = {
    name: 'Some name',
    address: 'Some address'
  };

  body.replaceText('{name}', client.name);
  body.replaceText('{address}', client.address);
}

UPDATE:

I created the following google apps script:

function doPost(e) {
  var body = DocumentApp.openById("JYOfA_Uv5fxLFA84g11H9XsizHo3F7e1FvSs3EG1vvo").getBody();
  var client = new Function("return " + e.postData.contents)();
  body.replaceText('{name}', client.name);
  body.replaceText('{address}', client.address);
  return ContentService.createTextOutput("Done.")
}

The cURL command I'm running is:

curl -L -d '{"name":"hello","address":"world"}' 'https://script.google.com/macros/s/AKfycbxF_1gddelneCdWCsFcvqT1OgU2zqkjTKSEIulSQfXjfgx1rnY/exec'

And the document that the google apps script calls basically has the information {name} and {address}.

The cURL command gives me error that the file could not be found.

The links are accessible as there aren't anything secret there. Only test stuff.


UPDATE 2:

Link to the test script: https://script.google.com/a/macros/klarna.com/d/1Q668U_HVtv3T_vQG_39oVL7V5YkcHzYhHUVhR8_XlOmUMRoPf15jDILK/edit?uiv=2&mid=ACjPJvEnpQ77A1Mo4sw8Y5IOPH5Pxs-IxEp1BFpfJU4RsfGXA7HbiHIv23ug_Rw0Y0dPKpWGM_Bb5oVznOqQMeyBki_jQb_g_LafNNPrLM0twO1eyPil3oqR-TjbHA8JToQQk4kdT1eZkE0&splash=yes

Link to the test document: https://docs.google.com/document/d/1JYOfA_Uv5fxLFA84g11H9XsizHo3F7e1FvSs3EG1vvo/edit

2
  • Would you clarify the question, please. Do you want to access your external data source from within the script via a cURL-like function? Or, are you trying to call the bound apps script from an external script via cURL? Commented Oct 18, 2017 at 22:05
  • I want to pass external data from within a cURL-like function and pass it to google docs :) Commented Oct 26, 2017 at 15:01

1 Answer 1

0

How about a following sample? I thought of using doPost() of Web Apps for your situation. In order to use this sample, please confirm the following flow.

Flow :

  1. Prepare Google Document as a sample. Please put {name} and {address} in there.
  2. Copy and paste the sample script (doPost(e)) to your script editor.
  3. Deploy Web Apps
    • On the Script Editor
    • Publish
      • Deploy as Web App
        • Create new Project version
        • At Execute the app as, select "your account"
        • At Who has access to the app, select "Anyone, even anonymous"
        • Click "Deploy"
        • Copy "Current web app URL"
        • Click "OK"
  4. Please put your endpoint to the sample curl command and run it.

The Current web app URL is the endpoint for curl command. The detail information of Web Apps is here. When you modify your script, Web Apps has to be redeployed as a new version.

Sample script :

function doPost(e) {
  var body = DocumentApp.openById("DOCUMENT_ID").getBody();
  var client = new Function("return " + e.postData.contents)();
  body.replaceText('{name}', client.name);
  body.replaceText('{address}', client.address);
  return ContentService.createTextOutput("Done.")
}

At Post method, JSON data can be sent. So I used this. var client = new Function("return " + e.postData.contents)(); is used for converting from the received string JSON data to an object.

Sample curl command :

curl -L -d '{name: "Some name", address: "Some address"}' 'https://script.google.com/macros/s/#####/exec'

Note :

This is a simple sample. So when you use this, please modify this to your environment.

If I misunderstand your question, I'm sorry.

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

12 Comments

Sorry for the late reply! It looks like the solution I want although something seems wrong, I'm getting "Google Drive - this page could not be found". I've checked the url and it is correct. Will try to see what I'm doing wrong..
@Kevin I think that there are 2 points for your situation. One is that after Web Apps was deployed, if the script was modified, Web Apps has to be redeployed as a new version. You can see how to deploy it from my answer. Another one is that recently, there are sometimes the cases that such error is returned, when Web Apps was deployed. At that time, I avoid the error by reopening the document with script. I don't know whether your situation is the same to mine. But can you try it? If these were not solutions, I'm sorry. At that time, can you show me your whole script at your shared script?
@Kevin And then, I have a question for your situation. Is your error the response from curl?
I updated my first post with the commands I'm running :) Tried reopening, redeploying as a new version and still getting the same response. The response is from the curl command.
@Kevin Thank you for adding your additional question. In your case, Web Apps might be not deployed. Can you redeploy it as NEW version?
|

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.