通过操作p refill url string

时间:2017-09-12 17:39:12

标签: javascript forms date google-apps-script

您好我已经研究了很多关于如何使用日期输入字段中的当前日期预填充谷歌表单的方法 我想出的最好的方法是创建一个预填充表单并使用预填充谷歌表单URL并动态更改url字符串以插入当前日期作为参数之一。 我尝试使用链接引用创建一个页面(谷歌应用程序脚本),我试图将链接href设置为包含当前日期参数但没有成功的新字符串。我已经有了一个函数(在code.gs中),它采用谷歌形式的URL并正确操作它来输入url字符串中的当前日期作为参数之一。但是在链接href中设置这个新的url字符串不起作用。

你能否就这个或更简单的方式在谷歌表格上预先填写当前日期提供建议(例如很棒)。 最受赞赏的。

/* 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;
       };

我的javascript部分

 <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 个答案:

答案 0 :(得分:0)

您可以尝试从

更改它吗?
window.getnowx2 = function() {
  google.script.run
  .withSuccessHandler(updateDisplay2)
  .getnowx();
  }

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

在第一个语句中,您要为变量分配函数的原因 window.getnowx2但不称呼它。在第二个声明中,您实际上会调用它。 对不起,我不太确定这一点,如果这不起作用,有更多知识渊博的人会解决这个问题。

答案 1 :(得分:0)

Per Chance在堆栈交换中遇到了一些代码并且它有效。很简单只是不知道如何。我的getnow函数采用prefill form url和string操作来插入当前日期,这样我就可以自动获取带有当前日期的google表单。

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;
   };