Matt
Matt

Reputation: 337

Post data to Google Spreadsheet via AppsScript (private deployment)

I am trying to post data using HTML forms or AJAX over to AppsScript attached to a Google Spreadsheet. I got the app to the state that it works with app deployed to "anyone, even anonymous".

But my goal is to create JS script or PHP which will use service account to authenticate and post data to this Spreadsheet.

I can't find any helpful documenation or example.

Here is my code: [HTML]

<!DOCTYPE html>
<html>
<body>

<form method="post" action="https://script.google.com/a/macros/domainname/s/AKfycbzVVBWfeffKo7OHdhAtjjgri0ouJ5qIkSjQTpsIISw8/exec">
  First name:<br>
  <input type="text" name="firstname" value="test1">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="test2">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>

[Apps Script]

function doPost(e) {

  try {
    var doc     = SpreadsheetApp.getActiveSpreadsheet();
    var sheet   = doc.getSheetByName("Sheet1");
    
    
    sheet.getRange(1,1).setValue(JSON.stringify(e.parameters));
    
  }
  catch(error) {
    Logger.log(e);
  }
  finally {
    return;
  }

}

I already created the service account and have the JSON file with clientID, Secrets etc. I imagine I need to append the POST with this data somehow but I am not sure how to achieve that so Google Apps will authenticate it.

The reason for this is that the company who wants this app cannot enable sharing documents to everyone who have the link for security reasons.

I need to publish with settings show below: enter image description here

If I set to "Anyone within domain" I get enter image description here

So I want to execute my Form somehow so it executes as a user within that domain.

Can someone help with some documentation URLs or some tutorial, etc?

Best Regards Matt

Upvotes: 2

Views: 898

Answers (1)

Zig Mandel
Zig Mandel

Reputation: 19834

There is no reason to use service accounts in this case, even with your restriction.

Publish the app to run as you, then all spreadsheet calls will use your credentials not the user that uses your webapp.

Also, its not possible to use 2-legged oauth with service accounts in apps script, it only supports 3-legged oauth.

Upvotes: 1

Related Questions