连接单引号标记Google Script

时间:2018-05-01 21:38:28

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

我有三列数据

selector                                            label     option list
time you personally have been engaged with uscan    label_1   Arts
time you personally have been engaged with uscan    label_2   Children’s Issues
time you personally have been engaged with uscan    label_3   Coaching
time you personally have been engaged with uscan    label_4   Community Development
time you personally have been engaged with uscan    label_5   Conflict
time you personally have been engaged with uscan    label_6   Consulting

我正在尝试连接这些列,以便在第4列中得到

option {
label: "Label_1;
selector: ["time you personally have been engaged with uscan"="Arts"];
}

option {
label: "Label_2;
selector: ["time you personally have been engaged with uscan"="Children’s Issues"];
}
etc

我的尝试

result[i] = [""option {label:""" + values[i][0] + "";" + "selector: [" + values[i][0] + ""=" +  + values[i][1] + ""];}"];

被所有必需的单引号玷污

谢谢

GS

function OptionsList() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OptionList");
  var lr = sheet.getLastRow();
  var values = sheet.getRange(2, 1, lr,3).getValues();
  var result = [];

  //Add items to results
  for(var i=0; i<lr; i++){

    result[i] = [""option {label:""" + values[i][0] + "";" + "selector: [" + values[i][0] + ""=" +  + values[i][1] + ""];}"];

}

//Post back to column 4 starting on row 2
sheet.getRange(2, 4, lr, 1).setValues(result);
}

1 个答案:

答案 0 :(得分:1)

这次修改怎么样?

修改要点:

  • 关于result[i]
    • "已转发为\"
    • 添加了换行符,如\n
  • filter()删除空单元格。

修改后的脚本:

function OptionsList() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OptionList");
  var lr = sheet.getLastRow();
  var values = sheet.getRange(2, 1, lr, 3).getValues();
  values = values.filter(function(e){return e[0] && e[1] && e[2]}); // Added
  var result = [];
  //Add items to results
  for(var i=0; i<values.length; i++){ // Modified
    result[i] = ["option {\nlabel: \"" + values[i][1] + ";\n" + "selector: [\"" + values[i][0] + "\"=\"" + values[i][2] + "\"];\n}"]; // Modified
  }
  //Post back to column 4 starting on row 2
  sheet.getRange(2, 4, result.length, 1).setValues(result); // Modified
}

注意:

  • 例如," "Label_1之后需要label: "Label_1;吗?如果需要,请修改如下。
    • + values[i][1] + "\";\n"

参考:

如果我误解了你的问题,请告诉我。我想修改它。