在HTML下载列表中获取一个带有Selected Option的var到Google Script Spreadshee

Get a var with Selected Option in HTML downlist to Google Script Spreadsheet

本文关键字:Option Selected var Spreadshee Script Google 一个 列表 下载 HTML 获取      更新时间:2024-06-06

我真的需要帮助我几乎完成了我的谷歌脚本,但我遇到了麻烦,我不知道如何从下载列表(html)中获得所选选项(所选月份),并将其存储在"var"中,我真的很感谢你的帮助。

我尝试使用getElementById,但没有成功。

谢谢。

文档代码.gs

/*** Retrieves all the rows in the active spreadsheet that contain data and logs the values for each row.
   * For more information on using the Spreadsheet API, see
   * https://developers.google.com/apps-script/service_spreadsheet*/
function Menu() {
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
       .createMenu("Christian's Menu")
       .addItem('Side Bar Comites', 'showSidebar')
       .addToUi();
 }
function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle('Sedesol - Captura de Entregas')
      .setWidth(250);
 SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}
function autowritemonthday(form){
   var storeMonth = getElementById('monthlist').value; /*Does not work i dont know why*/
   Browser.msgBox(storeMonth); /*tried to view the value from var storeMonth*/
 /*I put this as comment because does not work either
    var e = document.getElementById('monthlist');
    var val = e.options[e.selectedIndex].text;*/
/*I need Storage the selected value from the downlist here before the   onSeach(form)*/
   onSearch(form); /*call the function to search the input text and put the cursor in the right Cell*/
   var hoja = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = hoja.getSheets()[0];
   var rows = sheet.getDataRange();
   var ss = hoja.getActiveCell()
   var selectedrow = ss.getRow();
   var selectedcol = ss.getColumn();
   var monthpos = sheet.getRange(selectedrow, 7); /* get the cell position for example monthpost(5,7)*/
   var daypos = sheet.getRange(selectedrow, 8);
   var tempm = form.mesinini; /*get the value of the inputbox by NAME*/
   var tempd = form.dias;
   var remspacestxtmonth = tempm.trim(); /*remove blank spaces*/ 
   var uppertxtmonth = remspacestxtmonth.toUpperCase();/*conv. to uppercase*/
   var remspacestxtday = tempd.trim(); /*remove blank spaces*/ 
   monthpos.setValue(uppertxtmonth); /*write the var value in the selected cell preview positioned by the function onSearch*/
   daypos.setValue(remspacestxtday); 
}
function doGet() {
  return HtmlService.createHtmlOutputFromFile("Page");
}
function onSearch(form){
    var tempc = form.comites;
    var comite = tempc.trim();
    /*Browser.msgBox(comite);*/
    var searchString = comite;
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("REYNOSA"); 
    var column =4; //column Index - where i am going to search the value of the var comite 
    var columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues(); //1st cell Titles
    var searchResult = columnValues.findIndex(searchString); //Cell Index - 2
    if(searchResult != -1)
    {
        //searchResult + 2 is row index.
        SpreadsheetApp.getActiveSpreadsheet().setActiveRange(sheet.getRange(searchResult + 2, 1))
    }
}
Array.prototype.findIndex = function(search){
  if(search == "") return false;
     for (var i=0; i<this.length; i++)
       if (this[i] == search) return i;
  return -1;
 } 
Array.prototype.findIndex = function(search){
  if(search == "") return false;
     for (var i=0; i<this.length; i++)
         if (this[i] == search) return i;
  return -1;
} 

page.html

<head>
    <title>Page</title>
</head>
<body>
    <div>
         <form>
          INGRESA LA FECHA DE ENTREGA QUE DESEAS UTILIZAR PARA LOS COMITES...  /EN/...WRITE THE DATE YOU ARE GOING TO USE TO CAPTURE THE RECEIPTS
         <br><br/>
            <select id="monthlist" name="monthlist">
            <option value="enero">JANUARY</option>
            <option value="febrero">FEBRUARY</option>
            <option value="marzo">MARCH</option>
            <option value="abril">APRIL</option>
            <option value="mayo">MAY</option>
            <option value="Junio">JUNE</option>
            <option value="julio">JULY</option>
            <option value="agosto">AUGUST</option>
            <option value="septiembre">SEPTEMBER</option>
            <option value="octubre">OCTOBER</option>
            <option value="noviembre">NOVEMBER</option>
            <option value="diciembre">DECEMBER</option>
            </select>
         <br><br/>
            Ingresa el MES [EJEMPLO: MARZO]: /EN/WRITE THE MONTH <input id="resp" type="text" value="" name="mesinini" />
         <br><br/>
            Ingresa el DIA /EN/WRITE THE DAY OF THE MONTH [EJEMPLO: 14]: <input id="resp" type="text" value="" name="dias" />
         <br><br/>
            CAPTURA EL COMITE. /EN/ CAPTURE THE RECEIPT NUMBER
         <br><br/>
            Ingresa el Comite: /EN/ WRITE THE RECEIPT NUMBER <input id="resp" type="text" value="" name="comites" />
         <br><br/>
            <input type="button" onclick="formautomd()" value="Capturar Entrega del Comite"/> /*THIS BUTTON*/
         <br><br/>         
            <input type="button" onclick="formonSearch()" value="Buscar Comite"/> 
         <br><br/>
            <input type="button" onclick="formexit()" value="Salir del Menu"/>
       </form>
     </div>  
</body>
<script type="text/javascript">
    function formonSearch() {
               /////even tried to put the getElementbyId here and same result.
        google.script.run.onSearch(document.forms[0]);
    }
    function formautomd() {
        google.script.run.autowritemonthday(document.forms[0]);
        } /* IT NEED BE STORAGED WHEN I PRESS THIS BUTTON*/
    function formexit(){
        google.script.host.close();
        }
</script>

getElementById()不能在.gs服务器代码中使用。.gs脚本在谷歌的服务器上运行。getElementById()仅适用于浏览器。您的功能:

function autowritemonthday(form){
   var storeMonth = getElementById('monthlist').value; 
  /*Does not work i dont know why*/

.gs代码中。getElementById()仅适用于HTML中的<script>标记。

所需的数据必须来自"form"对象。

这就是你需要使用的:

var storeMonth = form["monthlist"];

要查看表单对象中的内容,请遍历表单对象,如下所示:

function autowritemonthday(form){
  Logger.log('form: ' + form);
  for (var key in form) {
    Logger.log('the key is: ' + key);
    Logger.log('the value is: ' + form[key]);
  };
  return;
};

提交表单,然后在代码编辑器中,选择VIEW菜单,然后选择LOGS。

我最初的答案:

你试过这样的东西吗:

function formautomyd() {
  var storeMonth = getElementById('monthlist').value;
  google.script.run.autofechaydia(document.forms[0]);
}

您声明您使用了:

getElementByID

但没有显示代码。末尾的字母"D"不能大写。必须是小写。getElementById()