{"id":259,"date":"2020-01-22T12:33:05","date_gmt":"2020-01-22T12:33:05","guid":{"rendered":"http:\/\/www.scm.keele.ac.uk\/staff\/stan\/?p=259"},"modified":"2020-01-22T12:33:05","modified_gmt":"2020-01-22T12:33:05","slug":"how-to-choose-a-random-file-from-a-google-drive-folder-and-open-it","status":"publish","type":"post","link":"https:\/\/machinemachines.org\/index.php\/2020\/01\/22\/how-to-choose-a-random-file-from-a-google-drive-folder-and-open-it\/","title":{"rendered":"How to use gscript to choose a random file from a Google Drive folder, and open it?"},"content":{"rendered":"<p>It&#8217;s not so hard, though the gscript has to be published as a web app. Write the doGet function to retrieve the URL of the file in Drive, then return a small script that redirects to the URL using HtmlService.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">function<\/span> doGet(e) {\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> folder <span style=\"color: #333333;\">=<\/span> DriveApp.getFolderById(<span style=\"background-color: #fff0f0;\">\"your-folder-id\"<\/span>);\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> files <span style=\"color: #333333;\">=<\/span> folder.getFiles();\n \n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> filearray <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #007020;\">Array<\/span>;\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> c<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\n \n  <span style=\"color: #008800; font-weight: bold;\">while<\/span>(files.hasNext()) {\n    <span style=\"color: #008800; font-weight: bold;\">var<\/span> s <span style=\"color: #333333;\">=<\/span> files.next();\n    filearray[c] <span style=\"color: #333333;\">=<\/span> s.getUrl();\n    c<span style=\"color: #333333;\">=<\/span>c<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\n  }\n \n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> item <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">Math<\/span>.round( <span style=\"color: #007020;\">Math<\/span>.random() <span style=\"color: #333333;\">*<\/span> filearray.length );\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> itemurl <span style=\"color: #333333;\">=<\/span> filearray[item];\n\n  <span style=\"color: #008800; font-weight: bold;\">var<\/span> html<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"&lt;script&gt;window.top.location.href='\"<\/span><span style=\"color: #333333;\">+<\/span>itemurl<span style=\"color: #333333;\">+<\/span><span style=\"background-color: #fff0f0;\">\"';&lt;\/script&gt;\"<\/span>;\n \n  <span style=\"color: #008800; font-weight: bold;\">return<\/span> HtmlService.createHtmlOutput(html);\n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not so hard, though the gscript has to be published as a web app. Write the doGet function to retrieve the URL of the file in Drive, then return a small script that redirects to the URL using HtmlService. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15&hellip;&nbsp;<a href=\"https:\/\/machinemachines.org\/index.php\/2020\/01\/22\/how-to-choose-a-random-file-from-a-google-drive-folder-and-open-it\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">How to use gscript to choose a random file from a Google Drive folder, and open it?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","iawp_total_views":568,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_src":null,"author_info":{"display_name":"as","author_link":"https:\/\/machinemachines.org\/index.php\/author\/stan\/"},"_links":{"self":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/posts\/259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/comments?post=259"}],"version-history":[{"count":0,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"wp:attachment":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}