xodus_fr has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am currently trying to switch to Net::Google::Spreadsheets::V4.

The following code is from the example in the module's files and shows how to write/update data into the sheet.

I would like to iterate the rows of the spreasheet.

Can you explain me how to iterate into google sheet, row by row ?

I am stuck.

Thank you very much.
my @rows = ( [qw(name age favorite exodus)], # header [qw(tarounina 31 curry has)], [qw(jirou 18 gyoza begun)], [qw(saburou 27 ramen NPG)], ); for my $row (@rows) { push @requests, { pasteData => { coordinate => { sheetId => $sheet_prop->{sheetId}, rowIndex => $idx++, columnIndex => 0, }, data => $gs->to_csv(@$row), type => 'PASTE_NORMAL', delimiter => ',', }, }; } ($content, $res) = $gs->request( POST => ':batchUpdate', { requests => \@requests, }, );

Replies are listed 'Best First'.
Re: Net::Google::Spreadsheets::V4 usage
by Marshall (Canon) on Aug 23, 2021 at 21:24 UTC
    Hi xodus_fr,

    To back up just a bit, my recommendation would be to first try to get the module's example code to run with minimal modifications (I guess you need to add your specific credentials, etc). The author gives a program to generate the credentials and then a program that uses them.

    You ask: Can you explain me how to iterate into google sheet, row by row ?. I have no experience with this module, but it appears from your code snippet, that is exactly what this code is doing!

    A straightforward reading of the code would indicate that this thing is going row by row, selecting the first cell in each row (row, col),(0,0),(1,0),(2,0), etc. and then writing columns starting in that cell and the to the right of that cell based upon a line in CSV format. The writing of the cells is not done right way, but put into a queue of @requests which are then later processed as a single batch operation.

    I presume that $idx is initialized to 0 before this code. coordinate is a reference to a hash with the sheet,x,y coordinates. data => $gs->to_csv(@$row), contains the data for multiple columns, presumably formatted into a single CSV line, by the to_csv method. @$row expands each reference to a row out into each element of the referenced array. (The reference to row is called simply row here, I would have called it $row_ref, but minor quibble)

    @rows is an Array of Arrays (a 2-D Array). Each element of @rows is a reference to an array which represents the columns on that particular row. The loop, for my $row (@rows) { will generate one pasteData request per row of data to be written. In this case each request will wind up writing 4 cells on each row, (name age favorite exodus), etc.

    So instead of a separate write for each individual cell, this code writes an entire row of 4 columns to the spreadsheet for each request. I presume that is done for efficiency.

    I hope this helps you understand what this code appears to do on the surface. Again, I have no experience with this particular module. I just saw that it was available and written years after the one that you were currently using.

    We never did figure out why your code that was running "broke". Could be problem with your Perl environment. Could be a problem with something that Google did. I have no idea.

    Again, I recommend running the example before trying to modify it for your own purposes.

      I think your are missing something.

      When I say "row", I don't mean @rows. I mean the row of a spreadsheet.

      The code snippet shows how to write cell content into rows into the spreadsheet.

      You usually would use a google spreadsheet populated though a google form.

      Then you would compile the data and transform it into something else (charts, spreadsheets etc..).

      Therefore, one of the main thing you need to do is read the content of a google spreadsheet.

      The code here directly populates a @rows variable with random data to write into the spreadsheet.

      My need is to start with an empty @rows variable and populate it from an existing spreadsheet.

      I am actually discussing with the author of the module and he's integrating such example in the next delivery !!! He understands the need.

      I'll update the thread with the example form the author.

      Thanks!
        Ok. I tried to play with this a bit more. Geez, even installing Net::Google:Spreadsheets::V4 took some work! I ran into a failed Furl installation dependency and had to back up 5 versions to Furl-3.08. Then I tripped across the issue at Perl Google Sheets API Install Problems. I did manage to create a Google Client_id and Secret, but for the moment have given up because I have some other work in progress. Evidently I have to spend some more time learning about the Google App eco-system.

        However, this example appears to be what you want:
        I can't run this critter myself, you hopefully you can.

        #!/usr/bin/env perl use strict; use warnings; use 5.010_000; use utf8; binmode STDOUT, ":encoding(utf8)"; use FindBin; use lib $FindBin::Bin . '/../lib'; use Net::Google::Spreadsheets::V4; use URI::Escape; my $gs = Net::Google::Spreadsheets::V4->new( client_id => "YOUR_CLIENT_ID", client_secret => "YOUR_CLIENT_SECRET", refresh_token => "YOUR_REFRESH_TOKEN", spreadsheet_id => "YOUR_SPREADSHEET_ID", ); my($content, $res); my $title = 'My sheet'; my $sheet = $gs->get_sheet(title => $title); # read data ($content, $res) = $gs->request( GET => sprintf("/values/%s", uri_escape($gs->a1_notation( sheet_title => $title, ))) ); for my $row (@{ $content->{values} }) { say join(', ', @$row); } exit;
        PS: If I were messing with this, I'd run Data::Dumper on $sheet,$content and $res and see what those things looks like.

        what do you get when get_sheet() is called? What kind of object is this? Does it contain the rows you need?