简体   繁体   English

将逗号分隔的字符串添加到数组

[英]Adding a comma separated strings to an array

I'm trying to add comma separated spreadsheet values to an array using Google Apps Script.我正在尝试使用 Google Apps 脚本将逗号分隔的电子表格值添加到数组中。 Let's call the cells I'm looking at A1, B1 and C1.让我们将我正在查看的单元格称为 A1、B1 和 C1。 All of them contain text, to be more precise these are the exact values:它们都包含文本,更准确地说,这些是确切的值:

A1="a"; A1="a"; B1="b,c,d"; B1="b,c,d"; C1="e" C1="e"

Using the function below I get the following result in my console使用下面的 function 我在控制台中得到以下结果

[[a,b,c,d,e]]

If I want to paste them back into the spreadsheet I end up with如果我想将它们粘贴回电子表格中,我最终会得到

A1="a"; A1="a"; B1="b"; B1="b"; C1="c"; C1="c"; D1="d"; D1="d"; E1="e"; E1="e";

This problem is probably related to the comma separated values in B1 because the array should actually look similar to this:这个问题可能与 B1 中的逗号分隔值有关,因为数组实际上应该类似于:

[["a","b,c,d","e"]]

Replacing the commas is no option.替换逗号是没有选择的。 Any ideas to tackle my problem are appreciated!任何解决我的问题的想法都值得赞赏!

Code snippet:代码片段:

function valuesToArray(){
            
var ss = SpreadsheetApp.getActiveSpreadsheet();
var worksheet = ss.getSheetByName("Worksheet");
var range = worksheet.getRange(1,1,1,3).getValues();

Logger.log(range);
    
}
function loadCsv(csv) {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Worksheet');
  const vs=Utilities.parseCsv(csv);
  sh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM