1
votes

Hello I have research many ways on how to prefill google form with a current date in the date input field.
The best i came up with is to create a prefill form and use the prefill google form url and dynamically change the url string to insert current date as one of the arguments. I tried creating a page(google app script) with a link reference that i am trying to set the link href to the new string containing current date argument but no success. I already have a function(in code.gs) that take the google form url and manipulates it correctly to input the current date in the url string as one of the arguments. But setting this new url string in the link href not working.

Can you advise(examples be great) on this or an easier way to prefill current date on a google form. Most most appreciated.

/* NOTE!!! Make a copy of this file - Choose "File" "Make a copy..."
 * Then publish this as a Web App - 
 * To Publish Choose the menu item "Publish" then "Deploy as Web App"
 * to see the Web App click the link named - "Test web app for your latest code"
 *
 */

var EXAMPLE_OF_GLOBAL_VARIABLE;//Use all capital letters so that you know it is a global
var n3;
function doGet() {
var template = HtmlService.createTemplateFromFile('Index');


// Build and return HTML in IFRAME sandbox mode.
return template.evaluate()
  .setTitle('Web App Window Title')
 .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  };

  //Attribute - This code is modified from the Google Help for a web app
    //Help - Welcome Screen - Web App
 function getFolderContents() {
 var contents,file,files,folderId,numFiles,topFolder;

 folderId = 'root'; //Set this as the default for an example
 topFolder;
 contents = {
  children: []
  };

 if (folderId == 'root') {
topFolder = DriveApp.getRootFolder();
} else {
// May throw exception if the folderId is invalid or app
// doesn't have permission to access.
topFolder = DriveApp.getFolderById(folderId);
}
contents.rootName = topFolder.getName() + '/';

 files = topFolder.getFiles();
 numFiles = 0;

while (files.hasNext() && numFiles < 20) {
file = files.next();
contents.children.push(file.getName());
numFiles++;
};

return contents; 
 };
function getnowx() {
var today = new Date();
 var dd = today.getDate();
 var mm = today.getMonth()+1; //January is 0!
 var yyyy = today.getFullYear();

if(dd<10) {
      dd = '0'+dd
      } ;
    if(mm<10) {
      mm = '0'+mm
      }; 
   today = yyyy + '-' + mm + '-' + dd;
  var n ='https://docs.google.com/forms/d/e/1FAIpQLSd5By6kDoO_T4WZWk5VjKs6WD5RVuC_06RX7jDv19jS-KR8sg/viewform?usp=pp_url&entry.1374707830=xxxx-xx-xx&entry.406706983&entry.1232084117&entry.769389513&entry.104847992';
     var n1 = n.replace("xxxx-xx-xx",today);  
      return n1;
       };

my javascript section

 <script>
     function updateDisplay(contents) {
     var headingText = "Displaying contents for " + contents.rootName + " 
    folder:";
     document.getElementById('main-heading').textContent = headingText;
     for (var i = 0; i < contents.children.length; i++) {
     var name = contents.children[i];
  document.getElementById('results').insertAdjacentHTML('beforeend', '<div>' 
  + name + '</div>');

   }

  function updateDisplay2(n5) {

   var linkButton = document.getElementById("link-button");
   linkButton.setAttribute('href', n5); 
   document.getElementById('link-button').textContent = linkText;
   }

  window.getTheData = function() {
  google.script.run
   .withSuccessHandler(updateDisplay)
  .getFolderContents();

    };

  window.getnowx2 = function() {
  google.script.run
  .withSuccessHandler(updateDisplay2)
  .getnowx();
  }
   </script>
2
Can you provide the url and also snapshot of code.gs - kaza
I copied a piece of online code and modifying it. - Safraz Mohammed
how do i add a snap shot to this this forum ? - Safraz Mohammed
trying added it here but error on text too long - Safraz Mohammed
Edit the question to add it - kaza

2 Answers

0
votes

Can you try changing it from

window.getnowx2 = function() {
  google.script.run
  .withSuccessHandler(updateDisplay2)
  .getnowx();
  }

to

  google.script.run
  .withSuccessHandler(updateDisplay2)
  .getnowx();

The reason being in the first statement you are assigning a function to a variable window.getnowx2 but not calling it. In the second statement you would actually call it. Sorry, I'm not too sure of this though, someone more knowledgeable will address this if this doesn't work.

0
votes

Per Chance came across some code in stack exchange and it worked. Very simple just did not know how. My getnow function take prefill form url and string manipulate to insert current date so i can get a google form with current date automatically.

function doGet() {
    var url = getnowx();
    return HtmlService.createHtmlOutput(
   "<a href='" + url + "?page=1'>Mylabel</a> <br><a href='" + url + "?
    page=1'>MyLABEL2</a>");

    };


function getnowx() {
   var today = new Date();
     var dd = today.getDate();
     var mm = today.getMonth()+1; //January is 0!
     var yyyy = today.getFullYear();

    if(dd<10) {
          dd = '0'+dd
          } ;
        if(mm<10) {
          mm = '0'+mm
          }; 
       today = yyyy + '-' + mm + '-' + dd;
      var n = 'https://docs.google.com/forms/d/e/1FAIpQLSd5BykDoO_T4WZWk5VjKs6WD5xxC_06RX7jDv19jS-KvR8sg/viewform?usp=pp_url&entry.1374707830=xxxx-xx-xx&entry.406706983&entry.1232084117&entry.769389513&entry.104847992';
     var n1 = n.replace("xxxx-xx-xx",today);  
    return n1;
   };