How to write to google Docs?


#1

I would like to know if there is any way that I can write to a Google Doc. My goal is to be able to have my R2 controller send out a daily report to Google Docs, which any of my coworkers can access. Currently, I have it set so I receive an e-mails once per day.


#2

Hello JOchao,
Welcome to the OptoForums!
Check out [U][B]this post[/B][/U] where I covered exactly that question (using IFTTT).
-OptoMary


#4

While I hesitate to go against Mary, I think there’s potentially a better way to do this…

Within Google Drive, it’s possible to create a Google Apps Script that you can publish as a web page. That script can do all sorts of fun things in Google Drive (and Gmail). Once you have your script set up, you can do a POST from within a chart or subroutine and pass it data. Here’s an example of a Google Script I’m using in this manner:

/**
 * Global objects.
 */
var enSpreadsheet = SpreadsheetApp.openById('xxx');

/*
 * Function to test storage function.
 */
function test() {
  
  // Create sample post data string.
  var testData = '123456,1,123,10,11,09/27/16,23:26:34,1,HINES,EN,123XYZ,';
  
  // Run storage function.
  storePostData(testData);
  
}

/*
 * Function to accept HTTP post. Runs function to store POST data in spreadsheet.
 */
function doPost(e) { storePostData(e.parameter.data); return ContentService.createTextOutput('OK'); }

/*
 * Parses post data into separate fields and loads data into bottom of EN spreadsheet.
 */
function storePostData(data) {
  
  // Parse data into fields. Data given as comma separated: SO, Load, Barrel, Minutes, Date, Time, Station, Customer, Part ID, Sub ID.
  var fields = data.split(',');
  
  // Position cursor to first empty row in spreadsheet.
  var logSheet = enSpreadsheet.getSheetByName('EN Log');
  var countRows = logSheet.getDataRange().getLastRow();
  var currentCell = logSheet.getRange('A1').offset(countRows, 0);
  while (!currentCell.isBlank()) {
    currentCell = currentCell.offset(1, 0);
  }
  
  // Load data into spreadsheet.
  currentCell.setValue(fields[5]);
  currentCell.offset(0, 1).setValue(fields[6]);
  currentCell.offset(0, 2).setValue('EN ' + fields[7]);
  currentCell.offset(0, 3).setValue(fields[0]);
  currentCell.offset(0, 4).setValue(fields[8]);
  currentCell.offset(0, 5).setValue(fields[10]);
  currentCell.offset(0, 6).setValue(fields[11]);
  currentCell.offset(0, 7).setValue(fields[1]);
  currentCell.offset(0, 8).setValue(fields[2]);
  currentCell.offset(0, 9).setValue(fields[3]);
  currentCell.offset(0, 10).setValue(fields[4]);
  
}

I’ve had great luck using this method, and it’s a bit more direct than IFTTT. One thing I don’t like about the IFTTT solution is that you can’t change the polling interval there, so if you need something happen in realtime, you’re out of luck. With my solution, everything happens in realtime. And with Google Apps Script, you can really easily interface with Gmail as well.


#5

Nice, thanks, varland! I’ve not yet played with Google Apps Script but it sounds like a handy tool. Back when I first pondered this question, you could do a POST (e.g. from a PAC) directly into google docs but then they (google) changed stuff around, as they do. Letting IFTTT worry about the changing google doc API was handy, but you’re right, it adds an extra piece that can even cost $$ etc.


#6

Hi varland,
I tried some scripting like what you show above, then published it as an API/web app, but how did you get a PAC to connect to your google code URL? I’m trying a few things w/the HttpGet command but haven’t got it working, yet. Getting 302 re-direct errors returned. Any clues on the glue from PAC to script would be most helpful!
Thanks,
Mary


#7

Mary… I often get a 302 status code, but I’m not necessarily treating it as an error because my data is POSTing correctly.