tulanejosh
tulanejosh

Reputation: 319

Accept multiple inputs from user and add to certain cells in a new row

I am trying to do the following in Google sheets using their script.

1) Accept multiple inputs from a user 2) Copy/paste the current last row to a new row underneath (the last row includes formulas I want to re-use) 3) Take the multiple inputs and fill out certain cells in the NEW last row.

For example, I have a column with labels: Product, Quantity, Cost, Tax, Ship, Buy. Product, Quantity, Cost are user inputs. Tax, Ship and Buy are formulas.

I want to prompt the user for "Product", "Quantity", "Cost" in one form.

Then I want to copy down the last row, and enter the results of "Product", "Quantity", "Cost" into the new last row.

Here is the script I have so far, I feel like I am so close:

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Evaluate')
      .addItem('Add New Row', 'addNewRow')
      .addItem('Compare', 'getData')
      .addToUi();
}

var ss = SpreadsheetApp.getActive();

function addNewRow() {
  var sh = ss.getActiveSheet(), lRow = sh.getLastRow(); 
  var lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);
  sh.insertRowsAfter(lRow, 1);
  range.copyTo(sh.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
}

function getData() {
  var ui = SpreadsheetApp.getUi();

  var result = ui.prompt(
      'Product',
      ui.ButtonSet.OK_CANCEL);

  var productName = result.getResponseText();

  var sh = ss.getActiveSheet();
  var lRow = sh.getLastRow(), lCol = sh.getLastColumn();

  var productRange = sh.getRange(lRow,1).setValue(productName);

}

I can't figure out: 1) How to create a prompt that accepts multiple inputs 2) How to place that in cells based on the column title (seems more efficient that way)

Thank you.

Upvotes: 3

Views: 4281

Answers (1)

Cooper
Cooper

Reputation: 64072

This is an example of how to create an HTML dialog that accepts more than one value. It has some other things in there also.

The Code.gs file:

function doGet()
{
  var html = HtmlService.createHtmlOutputFromFile('index');
  return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)

}

function showDialog()
{
    var ui=HtmlService.createHtmlOutputFromFile('index');
    SpreadsheetApp.getUi().showModelessDialog(ui,'Title');
}

function getData(a)
{
  var ts = Utilities.formatDate(new Date(), "GMT-6", "M/d/yyyy' 'HH:mm:ss");
  a.splice(0,0,ts);
  var ss=SpreadsheetApp.openById('SPREADSHEETID')
  ss.getSheetByName('Form Responses 1').appendRow(a);
  return true;
}

function getURL()
{
  var ss=SpreadsheetApp.openById('SPREADSHEETID');
  var sht=ss.getSheetByName('imgURLs');
  var rng=sht.getDataRange();
  var rngA=rng.getValues();
  var urlA=[];
  for(var i=1;i<rngA.length;i++)
  {
    urlA.push(rngA[i][0]);
  }
  return urlA;
}

The index.html file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <div id="data">
    <br />Text 1<input name="t1" type="text" size="15" id="txt1" placeholder="Text 1" />
    <br />Text 2<input name="t2" type="text" size="15" id="txt2" placeholder="Text 2" />
    <br />Text 3<input name="t3" type="text" size="15" id="txt3" placeholder="Text 3" />
    <br />Text 4<input name="t4" type="text" size="15" id="txt4" placeholder="Text 4" />
    <br /><input type="radio" name="Type" value="Member" checked />Member
    <br /><input type="radio" name="Type" value="Guest" />Guest
    <br /><input type="radio" name="Type" value="Intruder" />Intruder
    <br /><input type="button" value="submit" id="btn1" />
    <br /><img id="img1" src="" alt="img1" width="300" />
  </div>
  <div id="resp" style="display:none;">
    <h1>Response</h1>
    <p>Your data has been received.</p>
  </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $(function() {
        $('#btn1').click(validate);
        $('#txt4').val('');
        $('#txt3').val('');
        $('#txt2').val('');
        $('#txt1').val('')
        google.script.run
          .withSuccessHandler(setURL)
          .getURL();
      });
      function setURL(url)
      {
        $('#img1').attr('src',url[0]);
      }
      function setResponse(a)
      {
        if(a)
        {
          $('#data').css('display','none');
          $('#resp').css('display','block');
        }
      }

      function validate()
      {
        var txt1 = document.getElementById('txt1').value || '';
        var txt2 = document.getElementById('txt2').value || '';
        var txt3 = document.getElementById('txt3').value || '';
        var txt4 = document.getElementById('txt4').value || '';
        var type = $('input[name="Type"]:checked').val();
        var a = [txt1,txt2,txt3,txt4,type];
        if(txt1 && txt2 && txt3 && txt4)
        {
          google.script.run
            .withSuccessHandler(setResponse)
            .getData(a);
            return true;
        }
        else
        {
          alert('All fields must be completed.');
        }
      }

      function loadTxt(from,to)
      {
          document.getElementById(to).value = document.getElementById(from).value;
      }

      function radioValue()
      {
        var radios = document.getElementsByName('genderS');
        for (var i = 0, length = radios.length; i < length; i++) 
        {
          if(radios[i].checked) 
          {
            return radios[i].value;
          }
        }
      }


     console.log('My Code');
   </script>
  </body>
</html>

Upvotes: 3

Related Questions