• Resolved nfaires

    (@nfaires)


    Hey,

    I’m really struggling to use queries to get data into charts from my Google Sheets onto my site. I have a form which inputs the data into the sheet. The site is now displaying a chart from that sheet, but I’m having a really hard time doing queries to display the data as the chart that I want. I’ve read all of the documentation and the forums and can’t really find what I’m looking for.

    My shortcode looks like this:
    [gdoc key="https://docs.google.com/spreadsheets/d/1c2nfL4-7Avk-2SD3vemN0vofOo7iSNQcTDfcOwJdgxY/edit#gid=215205300" gid="215205300" chart="column" QUERY="select G, H,I"]

    It is displayed on this page:
    https://carrotpl.us/farm-data/

    According to the docs, I can use Google’s query language to get and add specific data that I want. However, as soon as I add Sum to the shortcode, it breaks the chart and it disappears. I’ve tried all kinds of versions of sum, such as doing it with parenthesis and without and no luck.

    I’d like the shortcode to do a sum of some columns. The other thing that seems to be happening is that I don’t know how to format the shortcode to display the info correctly in the first place. In the example above, there should be 3 columns displayed with 3 different crops, not just Berries. It’s a very simple spreadsheet with each entry displayed in a row (you can see that on the same page), but I just want to pull what I want from it.

    I’m not super familiar with queries which isn’t helping. Can someone help?

    https://www.ads-software.com/plugins/inline-google-spreadsheet-viewer/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Meitar

    (@meitar)

    So, I think you’re making this more complicated for yourself than it needs to be. As described here, don’t try creating a query to calculate your generated data at the same time as you try to format that data into a structure the chart you’re trying to draw expects. That’s really complicated. Instead, just create a new sheet that generates your chart-able data and then grab that data from its sheet using this plugin. Again, read the linked post for some more guidance.

    Thread Starter nfaires

    (@nfaires)

    Thanks for the quick reply. I had hoped to avoid that route because there’s more than 40 columns to deal with which can be a little tedious. But I did start creating that and it worked so that’s good.

    If I can ask one more question, somewhere in the documentation it mentioned that it’s possible to call the logged in member’s information. I have no idea how to do this and I would love to use the user’s email address to get information from the table for a table. How does that work with the short code?

    Plugin Author Meitar

    (@meitar)

    somewhere in the documentation it mentioned that it’s possible to call the logged in member’s information. I have no idea how to do this and I would love to use the user’s email address to get information from the table for a table. How does that work with the short code?

    This is done by implementing the plugin’s gdoc_query hook. See here:

    It’s just a standard WordPress filter, which you can/should learn about in the Codex. If, for example, you wanted to do a GQL query on a Google Spreadsheet but select only rows that match the current user’s email address, you might write a gdoc shortcode like this:

    [gdoc key="ABCDEFG" query="select A, B, C where X = '__WORDPRESS_USER_EMAIL__'"]

    and then, in your plugin or theme or whatever, a WordPress filter function like this:

    function filter_gdoc_query ($query, $atts) {
        if ('ABCDEFG' !== $atts['key']) { return $query; }
        $cur_user = wp_get_current_user();
        return str_replace('__WORDPRESS_USER_EMAIL__', $cur_user->user_email, $query);
    }
    add_filter('gdoc_query', 'filter_gdoc_query', 10, 2);

    In this example, when a logged-in user whose email address is [email protected] loads the page on which a gdoc shortcode with the key of ABCDEFG is inserted, the resulting query to the Google Sheet will be:

    select A, B, C where X = '[email protected]'

    All other gdoc shortcodes will simply be passed on unchanged.

    Hope this helps.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trouble creating charts using queries’ is closed to new replies.