Z
Z
zed1cus2015-01-20 17:46:11
API
zed1cus, 2015-01-20 17:46:11

How to access google spreadsheets?

Hello. There is a price list in the spreadsheets format in public access, with which it is possible to work if you log in from your account through the web interface. But how to programmatically, for example, using PHP, access the document in order to read the data? Document settings cannot be changed. I didn't find the answer in the documentation . Can eat a simple example or article?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Ivanov, 2015-03-05
@oshliaer

An example of how this can be solved through Google Apps Script. As the recipient of the XMLHttpRequest.
Essence of the method in the posted script file How to access google spreadsheets? . He

  1. Receives a POST request
  2. Gets data from a Table that is owned by another user. Access level "For everyone on the Internet".
  3. Returns a response.

Page hosting on Google Drive no longer works. You need to upload the page to your hosting.

M
Matvey Pravosudov, 2017-07-24
@oxyberg

I think the Facade pattern will do , which will provide a single point of entry for the component.

K
kpa6uu, 2017-07-24
@kpa6uu

The strategy will solve the problem.
Each formatter is a separate class. They all implement the same interface.
You can get them through a factory that implements a cache of object instances.

<?php

class FormatterFactory
{
  const TRAFFIC = 1;
  const CONVERSION = 2;

  protected static $cache = array();

  public static function get($formatterId)
  {
    if (static::isInCache($formatterId)) {
      return static::getFromCache($formatterId);
    }

    $formatter = static::createNewFormatter($formatterId);
    static::setToCache($formatterId, $formatter);

    return static::getFromCache($formatterId);
  }

  protected static function createNewFormatter($formatterId)
  {
    switch ($formatterId) {
      case static::TRAFFIC:
        $object = new TrafficFormatter();
        break;

      case static::CONVERSION:
        $object = new ConversionFormatter();
        break;

      default:
        throw new Exception("Unknown formatterId: {$formatterId}");
        break;
    }

    return $object;
  }

  protected static function isInCache($formatterId)
  {
    return isset(static::$cache[$formatterId]);
  }

  protected static function setToCache($formatterId, FormatterInterface $formatter)
  {
    static::$cache[$formatterId] = $formatter;
  }

  protected static function getFromCache($formatterId)
  {
    return isset(static::$cache[$formatterId]) ? static::$cache[$formatterId] : null;
  }
}

interface FormatterInterface
{
  public function format($data);
}

class TrafficFormatter implements FormatterInterface
{

  public function format($data)
  {
    return $data . 'traffic';
  }
}

class ConversionFormatter implements FormatterInterface
{
  public function format($data)
  {
    return $data . 'conversion';
  }
}

class Formatter
{
  /**
   * @param FormatterInterface[] $formatters
   * @param string $data
   * @return string
   */
  static public function format($formatters, $data)
  {
    foreach ($formatters as $formatter) {
      $data = $formatter->format($data);
    }

    return $data;
  }
}

$data = 'hello';

$data = Formatter::format(array(
  FormatterFactory::get(FormatterFactory::TRAFFIC),
  FormatterFactory::get(FormatterFactory::CONVERSION),
), $data);

echo $data; // hellotrafficconversion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question