Niya
Niya

Reputation: 79

Run server-side code with google chart select

I have a google visualization table that I'm publishing in a web app.

Background: I run a script that lists all the documents in a google folder in a spreadsheet. I then push that list into the google table I have published in the web app.

The need: I want to manage those that same list of documents directly from the web app. I want to be able to to move the document from one folder to another when I select the applicable row on the table.

Two things I have accomplished:

  1. I have a script that will move the document to a specific folder on the google drive using it's doc id.
  2. I have an event listener on the table so that when you click a row and then click the delete icon, you get a prompt that asks, "are sure you want to archive [enter document name]?" When I click ok, I get my test prompt that says "document archived". When I click no, I get my test prompt that says "request cancelled". So from this, I know I have the appropriate code (at least that's how it seems).

What I'm struggling with: I can't seem to get the codes above to work together. The event listener is providing me the url of the document which I have parsed to give me only the id. This is what I was hoping to use to get the rest of the code to run, but I think because I'm trying to interact with the server-side from the client-side, it's not working. Can anyone help me figure it out? I know that I need to use google.script.run.withSuccessHandler when running a server side script from the client side, but I don't know how it applies to this case the docid I need is being collected on table select. Any help is appreciated and I hope the above makes sense!

   // Draw Dashboard
        h2dashboard.bind([h2stringFilter, h2typeFilter], [h2chart]);
        h2dashboard.draw(h2dataView);


google.visualization.events.addOneTimeListener(h2chart, 'ready', function() {
       google.visualization.events.addListener(h2chart.getChart(), 'select', function() {
                  var selection = h2chart.getChart().getSelection();
                  var dt = h2chart.getDataTable();
                  // Get Value of clicked row
                    if (selection.length) {
                     var item = selection[0];
                     var docurl = dt.getValue(item.row, 1);
                     var docname = dt.getValue(item.row, 0);
                     var source = dt.getValue(item.row, 3);
                     // When button is clicked, show confirm box with value
                       $(document).ready(function() {
                         $("#hu2archive").on("click", function() {
                       var answer = confirm("Are you sure you want to archive " + docname + "?");
                   if (answer === true) {
                     var archive = DriveApp.getFolderById("FOLDER ID");
                     var docid = docurl.match(/[-\w]{25,}/); // This is where I'm grabbing the value from the row.
                     var doc = DriveApp.getFileById(docid);
                        doc.makeCopy(archive).setName(doc.getName());
                         source.removeFile(doc);
                          alert(docname + " has been archived!");
                           } else { 
                              alert("Request cancelled"); 
                           }
                         });
                        });
                      }
                  }); 
               });

Upvotes: 0

Views: 93

Answers (1)

Niya
Niya

Reputation: 79

I just got it! What I was having a hard time understanding was how to pass a variable from the client side to code.gs. I have only run a script in code.gs from the client side on button submit but never passed anything back.

So I ended up changing my code to the below which passes the variable I need into a successhandler where archiveDoc is the function in my code.gs and docurl is the name of the variable I need to pass from the eventlistener.

if (answer === true) { google.script.run.withSuccessHandler(onSuccess).withFailureHandler(err).archiveDoc(docurl);

I'm still new to coding so I just learned something new! So thanks Spencer Easton. I did in fact answer my own question.

Upvotes: 1

Related Questions