无法将我的复选框值转到电子表格

时间:2015-04-20 18:52:44

标签: google-apps-script

我知道Ui服务已在12月被弃用,但我的功能仍然有用。但是,我觉得基本脚本不再适用于我。我正在尝试将复选框的值记录到电子表格中,因此我使用方法e.parameter.checkbox1但我的脚本找不到该值。如下所示:当我尝试将复选框的值传递到我的电子表格中时,它会一直给我一个“FALSE”返回,我不知道为什么。有什么帮助吗?

function doGet(e){
var app = UiApp.createApplication();
var grid = app.createGrid(40,5).setId('grid');
var gridCart = app.createGrid(35,2).setId('gridCart');
var flyerOptionsGrid = app.createGrid(4,15).setId('flyerOptionsGrid').setStyleAttribute("border","2px solid #C0C0C0");
var panel = app.createHorizontalPanel();

var checkbox1 = app.createCheckBox("checkbox1").setId('checkbox1').setStyleAttribute('fontSize','10');
grid.setWidget(5, 1, checkbox1);

var addToCart = app.createButton('Add to Cart').setId('addToCart');
var addToCartButtonHandler = app.createServerClickHandler('addToCartButtonHandler');
addToCartButtonHandler.addCallbackElement(grid);
addToCart.addClickHandler(addToCartButtonHandler);
grid.setWidget(10, 1, addToCart);


panel.add(grid)
panel.add(gridCart)
app.add(panel)
return app;
}


function addToCartButtonHandler (e) {
var app = UiApp.getActiveApplication();

var user = Session.getActiveUser().getEmail()  
var savedCartSS = SpreadsheetApp.openById('1XRrJDHkHT0Rx7DROU0LESuRfCOZoErgJU40UjaKZ7Do');
var userCart = savedCartSS.getSheetByName(user)
var lastRow = userCart.getLastRow()

if (e.parameter.checkbox1 == 'true') {userCart.getRange(lastRow+1, 5, 1, 1).setValue("TRUE")} else {userCart.getRange(lastRow+1, 5, 1, 1).setValue("FALSE")}
return app;
}

1 个答案:

答案 0 :(得分:1)

如果要转换为HTML服务,则会复制代码。它成功获取复选框的值。

Code.gs

function doGet() {

  var buyForm = HtmlService
    .createTemplateFromFile('purchase')
    .evaluate()
    .setTitle('Buy Stuff')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME);

  return buyForm;
};

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .getContent();
};

function InsertInSS(argChkBox) {
  Logger.log('argChkBox: ' + argChkBox);
  var user = Session.getActiveUser().getEmail()  
  var savedCartSS = SpreadsheetApp.openById('spreadsheet ID');
  var userCart = savedCartSS.getSheetByName(user)
  var lastRow = userCart.getLastRow()

  if (argChkBox === true) {
    Logger.log('Was on');
    userCart.getRange(lastRow+1, 5, 1, 1).setValue("TRUE");
    return true;
  } else {
    userCart.getRange(lastRow+1, 5, 1, 1).setValue("FALSE");
    return false;
  };
};

purchase.html

<div>
  <input type='checkbox' id='idChkBox1'>
  <input type='button' value='add to cart' onmouseup='runGoogleScript()'>
</div>

<?!= include('SCRIPT_addToCart'); ?>

SCRIPT_addToCart.html

<script type="text/javascript">
  function onSuccess(argReturnValue) {
    alert('was successful ' + argReturnValue);
  };

  function runGoogleScript() {
    console.log('runGoogleScript ran!');

    var chkBoxValue = document.getElementById('idChkBox1').checked;

    google.script.run.withSuccessHandler(onSuccess)
      .InsertInSS(chkBoxValue);
  };
</script>