0

I have a case where:

  1. I have a Drive-folderId and the folder contains some zip files. And the zip files further contains some .docx files.
  2. Next, I want to search for a specific .docx in any of the zip files in the said folder.(say the file is A.docx)
  3. If found, I have to send that file, A.docx, as GMail Attachment using GMail API (I can use file.getWebViewLink() for this)

The problem is at the 2 point. (Extracting multiple zip files and then searching for files)

My Options

  1. Use Apps Script API to extract the files of zip files and then search for A.docx.
  2. Download all the zip files from the folder and extract it locally and then find the A.docxand then send it as attachments in Gmail API.

Now, kindly suggest which will be the better way to go?

Also, found this article to use Google-Apps-Script. Can I get some articles to refer to for GAS as the Google documentation is pretty limited? Thanks!

EDIT

Java Code:

StringBuilder sb = new StringBuilder();
        InputStreamReader in = null;

        URL url = new URL("https://script.google.com/a/<id>/exec?param1=HelloWorld");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        if (connection.getInputStream() != null) {

            System.out.println(connection.getInputStream());
            in = new InputStreamReader(connection.getInputStream(),Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }

        System.out.println(sb.toString());

GAS Code:

function doGet(e) {
  var params = JSON.stringify(e);
  var param = e.queryString;
  return param+"Received";
}

I want to fetch returned String output but conn.getInputStream() currently returns an empty html form. What am I doing wrong here?

Current Scenario

After running both the fore-mentioned codes(Java and GAS), when I sysout the connection.getInputStream(), all I get in console is an HTML markup with title Google - Sign in. What I conclude is that it is asking for o-auth. But should a script ask for it? How to proceed further from this?

What I simply want is pass some parameters such as folderId and fileName such as A and to my GAScript doGet(e), I want to traverse the files of the passed folderId and then extract all the zipped files to find A.docx. If found, I want to unzip it in a seperate folder and hence attach it in a mail(I can handle the mailing part, just want to extract the required docx).

  • Although I'm not sure about the file size of the zip files, when the file size of the zip files is less than 50 MB, from `I can use file.getWebViewLink() for this`, I would like to propose to use Google Apps Script. In this case, the file is not required to be downloaded and the URLs of `file.getWebViewLink()` can be directly retrieved. And as another method for running Google Apps Script, there is the Web Apps. [Ref](https://developers.google.com/apps-script/guides/web) In this case, the settings are simpler than that of Google Apps Script API. – Tanaike Sep 11 '20 at 06:16
  • Okay, So publishing a script in google can be done. And then I can hit the published URL with java with some params. Sounds interesting. –  Sep 11 '20 at 06:28
  • ```URL url = new URL("https://script.google.com/"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect();``` How can I hit this URL with some params of my folderID and fileName and fetch the data?@Tanaike –  Sep 11 '20 at 06:29
  • Thank you for replying. I think that your understanding is correct. As an important point, in the current stage, the maximum number of concurrent requests is 30. https://stackoverflow.com/q/17512194/7108653 About `How can I hit this URL with some params of my folderID and fileName and fetch the data?`, do you ask about the Google Apps Script for achieving it? For example, if you want to know the method for accessing to the Web Apps, is this useful for your situation? https://gist.github.com/tanaikech/a72aab0242012362c46ec69031c720d5 – Tanaike Sep 11 '20 at 06:33
  • Let me give it a try first, and then I will let you know if I fail or stuck at something. Many thanks for introducing me to ```Google Apps Script with Web Apps```. –  Sep 11 '20 at 06:38
  • Thank you for replying. If you need the support for resolving about the Google Apps Script, feel free to tell us. – Tanaike Sep 11 '20 at 07:34
  • @Tanaike Kindly see the updated edit code in the question. Thanks. –  Sep 11 '20 at 07:53
  • Thank you for replying. In your situation, the script of Java (the client side) works. If my understanding is correfct, please modify `return param+"Received";` to `return ContentService.createTextOutput(param+"Received");` and test it again. But in this case, when you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this. And also, this document might be useful.https://github.com/tanaikech/taking-advantage-of-Web-Apps-with-google-apps-script – Tanaike Sep 11 '20 at 07:59
  • By the way, when it requests to `https://script.google.com/a//exec?param1=HelloWorld`, at `doGet(e)`, the value of `HelloWorld` of the query parameter can be retrieved by `e.parameter.param1`. Also please be careful this. – Tanaike Sep 11 '20 at 08:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221308/discussion-between-ajay-dhiman-and-tanaike). –  Sep 11 '20 at 08:42
  • This URL is giving me the Google authorization form. Shouldn't this script be executed without the auth?@Tanaike –  Sep 11 '20 at 08:44
  • Thank you for replying. Unfortunately, I cannot understand about `This URL is giving me the Google authorization form. Shouldn't this script be executed without the auth?`. This is due to my poor English skill. I deeply apologize for this. Can I ask you about the detail of it? – Tanaike Sep 11 '20 at 08:45
  • Do I need to get a Google authorization check before calling my script URL because the conn.getInputStream() is fetching a Google-Sign in form in the console? If so, how? –  Sep 11 '20 at 08:50
  • Thank you for replying. From your updated question, I cannot understand about your settings of Web Apps. But if the login screen for Google is returned, please request it including the access token in the request headers. By this, it can request to the Web Apps. https://github.com/tanaikech/taking-advantage-of-Web-Apps-with-google-apps-script#howtoaccesstowebapps But before you use access token, using `Who has access to the app: Anyone, even anonymous`, I want to recommend for confirming whether both scripts of the client and the server (Web Apps) works. – Tanaike Sep 11 '20 at 09:03
  • Can you edit the post with the updates/accomplishments you have made thanks to @Tanaike comments, and update the situation you are in and the issue you have? – Kessy Sep 11 '20 at 13:19
  • @Kessy, Done! Please view the question. –  Sep 11 '20 at 13:56
  • @Ajay Dhiman At first, can I ask you whether both the client side (Java) and server side (Web Apps) worked under `Who has access to the app: Anyone, even anonymous`? When this could be done and you want to use the access token, it is required to retrieve the access token. But if you cannot retrieve the access token, for example, you can also use the key like the API key as the query parameter. In this case, you can use `https://script.google.com/macros/s/#####/exec?key=value` But I'm not sure about your goal. So this was not the direction you expect, I apologize. – Tanaike Sep 12 '20 at 01:57
  • Hi, apologies for not understanding it, but can you provide more information on the sign in issue you are having now? What is happening? – Kessy Sep 18 '20 at 06:18

0 Answers0