Insert newlines into Google Spreadsheet via cURL / PHP - HOW?

Is there a script or tutorial on how to do this? How to implement inserting data from my PHP application into a Google spreadsheet?

I looked at the API ( https://developers.google.com/google-apps/spreadsheets/ ) and there are no PHP codes / examples.

I tried implementing the Zend library, but something seems very outdated with this approach, as I am getting some silly errors in a simple line insert.

I don’t think I can use the Google Apps script ( https://developers.google.com/apps-script/reference/spreadsheet/ ) since I don’t see how I can code and trigger from a PHP script.

Anything else?

Isn't there a workable API for this that we can easily use and use?

+4
source share
1 answer

I don’t think I can use Google Apps Script because I don’t see how I can encode and run a script from PHP

Sure. You can create a web service using Google Apps Script, which can receive and process service requests. Just pass parameters to the web service, this can be done using cURL, for example.

The following example is for a simple table with two columns. You can add the following lines:

curl -L <SCRIPT URL>?col1='Value for column 1'&col2='Another value' 

or browser url:

 https://<SCRIPT URL>?col1='Value for column 1'&col2='Another value' 

In this example, the spreadsheet is published here , and the following is an example of a URL that you can use to add a row:

 https://script.google.com/macros/s/AKfycbzVDFmOeaQc4mDZhWCnwf0CUnX64YNhhnKIlTYhqtpBraINUf9e/exec?col1='Value for column 1'&col2='Another value' 

Instructions

Here is a step by step.

  • Open the Google spreadsheet, make sure that the spreadsheet is fully accessible (public).
  • Tools → Script Editor
  • Copy the code below and paste into the editor
  • Replace --spreadsheet-id-- with your sheet id (easy to find this)
  • File → Save
  • Publish → Deploy as a web application → Version = NEW; set access as needed
  • Log in (tell you about loans) → DEPLOY
  • You will now get the script url
  • Attach ?col1='Colum1Data'&col2='Colum2Data' to the URL or edit as needed to move the data to the spreadsheet from the URL
  • DONE

(Thanks to vr00n.)

Code.gs

This can be a stand-alone or container-related script; In any case, you need to provide a spreadsheet identifier, since the web application does not start in the context of the spreadsheet. Instead of displaying html, it uses a ContentService to serve plain text - you can clarify that this is appropriate for your application anyway.

 function doGet(e) { Logger.log( JSON.stringify(e) ); // view parameters var result = 'Ok'; // assume success if (e.parameter == undefined) { result = 'No Parameters'; } else { var id = '--spreadsheet-id--'; // Spreadsheet id var sheet = SpreadsheetApp.openById(id).getActiveSheet(); var newRow = sheet.getLastRow() + 1; var rowData = []; for (var param in e.parameter) { Logger.log('In for loop, param='+param); var value = stripQuotes(e.parameter[param]); //Logger.log(param + ':' + e.parameter[param]); switch (param) { case 'col1': rowData[0] = value; break; case 'col2': rowData[1] = value; break; default: result = "unsupported parameter"; } } Logger.log(JSON.stringify(rowData)); // Write new row to spreadsheet var newRange = sheet.getRange(newRow, 1, 1, rowData.length); newRange.setValues([rowData]); } // Return result of operation return ContentService.createTextOutput(result); } /** * Remove leading and trailing single or double quotes */ function stripQuotes( value ) { return value.replace(/^["']|['"]$/g, ""); } 

You should read the Google Content Service documentation, especially redirect warnings and confidential information.

+5
source

Source: https://habr.com/ru/post/1493454/


All Articles