【问题标题】:Why this try of recovering an array from the `e.parameter.variableName` method is not working?为什么这种从 `e.parameter.variableName` 方法恢复数组的尝试不起作用?
【发布时间】:2015-01-15 23:04:28
【问题描述】:

这段代码出了什么问题?当我记录数组列表时,在 sumbit 函数之前,它没问题(具有预期的内容和数组内元素的位置)。但是,当我通过e.parameter.arrayList得到它时,它没有相同的值,也不是元素。如何解决?

function showList(folderID) {
  var folder = DocsList.getFolderById(folderID);
  var files = folder.getFiles();
  var arrayList = [];
  for (var file in files) {
    file = files[file];
    var thesesName = file.getName();
    var thesesId = file.getId();
    var thesesDoc = DocumentApp.openById(thesesId);
    for (var child = 0; child < thesesDoc.getNumChildren(); child++){
    var thesesFirstParagraph = thesesDoc.getChild(child);
    var thesesType = thesesFirstParagraph.getText();
      if (thesesType != ''){
         var newArray = [thesesName, thesesType, thesesId];
         arrayList.push(newArray);
         break;
         }
      }
   }
    arrayList.sort();
    var mydoc = SpreadsheetApp.getActiveSpreadsheet();
    var app = UiApp.createApplication().setWidth(550).setHeight(450);
    var panel = app.createVerticalPanel()
                   .setId('panel');

    var label = app.createLabel("Choose your theses").setStyleAttribute("fontSize", 18);
    app.add(label);
    panel.add(app.createHidden('checkbox_total', arrayList.length));
    panel.add(app.createHidden('arrayList', arrayList)); 
    Logger.log(" arrayList before submit = " + arrayList);


    for(var i = 0; i < arrayList.length; i++){      
      var checkbox = app.createCheckBox().setName('checkbox_isChecked_'+i).setText(arrayList[i][0]);
      Logger.log("arrayList[i][0] = " + arrayList[i][0]);
      Logger.log("arrayList[i] ====> " + arrayList[i]);
      panel.add(checkbox);
   }
   var handler = app.createServerHandler('submit').addCallbackElement(panel);
   panel.add(app.createButton('Submit', handler));
   var scroll = app.createScrollPanel().setPixelSize(500, 400);  
  scroll.add(panel);
  app.add(scroll);
  mydoc.show(app);


}

function include(arr, obj) {
    for(var i=0; i<arr.length; i++) {
        if (arr[i] == obj) // if we find a match, return true
            return true;    }
    return false; // if we got here, there was no match, so return false
 }

function submit(e){
   Logger.log(" arrayList = " + arrayList);
  var arrayList = e.parameter.arrayList;
  var numberOfItems = Number(e.parameter.checkbox_total);
  var thesesArrays = [];
  var usedThesesType = [];
  var usedThesesName = [];
  for(var i = 0; i < numberOfItems; i++){
    if(e.parameter['checkbox_isChecked_'+i] == 'true'){
     Logger.log(" arrayList inside for loop = " + arrayList);
      Logger.log(" arrayList[i] = " + arrayList[i]);
      thesesArrays.push(arrayList[i]);
      usedThesesType.push(arrayList[i][1]);
            Logger.log(" arrayList[i][1] = " + arrayList[i][1]);

      usedThesesName.push(arrayList[i][0]); 
       Logger.log(" arrayList[i][0] = " + arrayList[i][0]);
     }
   }
  var allThesesTypeArray = []; // To control Theses type apparence in the final doc
  for (var i = 0; i < arrayList.length; i++) {
     var thesesType = arrayList[i][1];
     if ( !(include(allThesesTypeArray, thesesType)) ){
        allThesesTypeArray.push(thesesType);     }
   }
  var targetDocId = userProperties.getProperty('targetDocId');
  for (var i = 0; i < thesesArrays.length; i++) {
     var thesesType = thesesArrays[i][1];
    Logger.log(" thesesArrays = " + thesesArrays);
     var thesesId = thesesArrays[i][2];
     importTheses(targetDocId, thesesId, thesesType);       
    }   
  cleanNotUsedThesesTitles(targetDocId, allThesesTypeArray, usedThesesType);
  if(userProperties.getProperty('atLeastOneTheseType') == 0){
      Browser.msgBox('There was no theses inside your model. Check it!');
  }  
  var joinAndInsert = userProperties.getProperty('joinAndInsert');  
  showURL(usedThesesName, joinAndInsert);
  return UiApp.getActiveApplication().close();
}

【问题讨论】:

  • 那么,submit(e) 函数是从与电子表格关联的表单触发的?并且您设置了一个触发器,函数名称为“提交”?
  • 你从e得到什么值?

标签: javascript google-apps-script google-sheets


【解决方案1】:

您不能简单地将数组传递到表单字段并获取该数组的值。这与您在 Get String Value of Blob Passed to e.parameter in Apps Script 上询问的问题完全相同。唯一的区别是现在您尝试传递一个数组而不是一个 Blob。

您只能将字符串作为字段的值传递,因此您需要将数据(arrayList)转换为字符串。然后你需要将该字符串转换回一个数组。使用 JSON.stringify() 和 JSON.parse() 执行此操作。

改变

panel.add(app.createHidden('arrayList', arrayList));

panel.add(app.createHidden('arrayList', JSON.stringify(arrayList)));

改变

var arrayList = e.parameter.arrayList;

 var arrayList = JSON.parse(e.parameter.arrayList);

【讨论】:

    【解决方案2】:

    您应该得到一个对象被传递并分配给e 参数。您可以遍历该对象以查看属性和值是什么:

    function submit(e) {
      Logger.log("submit ran: " + e);
      Logger.log("values?: " + e.values);
    
      for(var propertyName in e) {
    
        Logger.log("propertyName: " + propertyName);
        Logger.log("This property value is: " + e[propertyName]);
        Logger.log(" ");
      }
    }
    

    【讨论】:

    • 是的,我已经做到了。但也没有,所以我必须弄清楚发生了什么
    【解决方案3】:

    我认为有一个错误,因为你写了:

    for (var file in files) {
        file = files[file];
    

    其中变量文件是数组的索引,因此您正在更改 bucle 中的值。 您是否尝试过使用其他名称,例如:

    for (var file in files) {
        f = files[file];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多