Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Google工作表中自动分隔和分隔字符串_Javascript_Google Sheets_Google Sheets Formula - Fatal编程技术网

Javascript 在Google工作表中自动分隔和分隔字符串

Javascript 在Google工作表中自动分隔和分隔字符串,javascript,google-sheets,google-sheets-formula,Javascript,Google Sheets,Google Sheets Formula,Google sheets有一个选项,可以从顶部菜单中选择,在指定字符时将文本分隔为列。可以使用逗号或其他字符 我正在寻找一个脚本,可以做这个过程自动为给定的列。有许多脚本可用于执行此操作,但我无法使用它们完成任务 我正在使用Android上的一个应用程序,它允许我扫描二维码,并将一串信息发送到谷歌表单 信息样本显示为:464839 | 362 | 2840 | 927 | 72 | 617 当信息发送到工作表时,我需要将这些信息分成单独的列。这个过程应该是自动的 我在搜索中发现了一段代码,但它

Google sheets有一个选项,可以从顶部菜单中选择,在指定字符时将文本分隔为列。可以使用逗号或其他字符

我正在寻找一个脚本,可以做这个过程自动为给定的列。有许多脚本可用于执行此操作,但我无法使用它们完成任务

我正在使用Android上的一个应用程序,它允许我扫描二维码,并将一串信息发送到谷歌表单

信息样本显示为:464839 | 362 | 2840 | 927 | 72 | 617

当信息发送到工作表时,我需要将这些信息分成单独的列。这个过程应该是自动的

我在搜索中发现了一段代码,但它对我不起作用

var range = SpreadsheetApp.getActiveRange();
var cell = range.getCell(1, 1); // Gets the cell A1 
var cellValue = cell.getValue(); // Gets the value of the cell A1

var cellArray = cellValue.split("|"); // Splits the cell value by ',' and  stores it in array.

//Iterate through the array
for(var i = 0; i < cellArray.length; i++){
    Logger.log(cellArray[i]);
}
var range=SpreadsheetApp.getActiveRange();
var cell=range.getCell(1,1);//获取单元格A1
var cellValue=cell.getValue();//获取单元格A1的值
var=cellValue.split(“|”);//将单元格值拆分为“”,并将其存储在数组中。
//遍历数组
对于(var i=0;i

我不是很懂代码,请帮忙

下面是您放置在上的代码,该代码以固定间隔运行,遍历a列中每一行的值,并尝试使用管道符号和替换拆分该值,然后沿该行中的列写入这些拆分值。如果以前已经这样做过,函数在尝试拆分值时将出错,因为不存在管道符号,但try/catch将捕获该错误并允许函数继续通过循环

    function myFunction() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("NameOFSheet"); //Change "NameOFSheet to the actual name of the sheet.
      var col1Values = sheet.getSheetValues(1, 1, sheet.getLastRow(), 1) //Gets 2d (2 dimensional) array of the Values in Column 1 (getSheetValues(startRow, startColumn, numRows, numColumns)_
      var splitVals = []; //Instantiate an empty 1d array variable
      //Iterate through the 2d array and set the values
      for(var i = 0; i < col1Values.length; i++){
        try{
          splitVals = col1Values[i][0].split("|"); //Creates a 1d array of values split at every occurrence of the pipe symbol ("|").
          //If there is no pipe symbol (which would be the case if this operation has already happened then the array will be blank because .split will throw an error which will get "caught" in the try catch and the row will be skipped

          //Iterate over the splitVals array and set the values of the columns along the row (i+1) that you are in.
          for(var col = 0; col < splitVals.length; col++){
            sheet.getRange(i+1, col+1).setValue(splitVals[col])
          }
        }catch(e){}

      }
    }
函数myFunction(){
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“NameOFSheet”);//将“NameOFSheet”更改为工作表的实际名称。
var col1Values=sheet.getSheetValues(1,1,sheet.getLastRow(),1)//获取第1列(getSheetValues(startRow、startColumn、numRows、numColumns)中值的二维(二维)数组_
var splitVals=[];//实例化一个空的1d数组变量
//迭代二维数组并设置值
对于(变量i=0;i

我对代码进行了注释以便于解释。我建议您继续阅读,以帮助您更好地理解它们和上面的代码。

下面是您放置在上的代码,该代码以固定间隔运行,并迭代列a中每行的值,尝试使用管道符号拆分值,然后替换,然后编写这些代码沿该行中的列拆分值。如果以前已执行过此操作,则函数在尝试拆分值时将出错,因为不存在管道符号,但try/catch将捕获该错误并允许函数继续通过循环

    function myFunction() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("NameOFSheet"); //Change "NameOFSheet to the actual name of the sheet.
      var col1Values = sheet.getSheetValues(1, 1, sheet.getLastRow(), 1) //Gets 2d (2 dimensional) array of the Values in Column 1 (getSheetValues(startRow, startColumn, numRows, numColumns)_
      var splitVals = []; //Instantiate an empty 1d array variable
      //Iterate through the 2d array and set the values
      for(var i = 0; i < col1Values.length; i++){
        try{
          splitVals = col1Values[i][0].split("|"); //Creates a 1d array of values split at every occurrence of the pipe symbol ("|").
          //If there is no pipe symbol (which would be the case if this operation has already happened then the array will be blank because .split will throw an error which will get "caught" in the try catch and the row will be skipped

          //Iterate over the splitVals array and set the values of the columns along the row (i+1) that you are in.
          for(var col = 0; col < splitVals.length; col++){
            sheet.getRange(i+1, col+1).setValue(splitVals[col])
          }
        }catch(e){}

      }
    }
函数myFunction(){
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“NameOFSheet”);//将“NameOFSheet”更改为工作表的实际名称。
var col1Values=sheet.getSheetValues(1,1,sheet.getLastRow(),1)//获取第1列(getSheetValues(startRow、startColumn、numRows、numColumns)中值的二维(二维)数组_
var splitVals=[];//实例化一个空的1d数组变量
//迭代二维数组并设置值
对于(变量i=0;i

我对代码进行了注释以便于解释。我建议您继续阅读,以帮助您更好地理解它们和上面的代码。

输出的样本数据在表中的何处,您希望数据沿哪一行分割?我希望数据沿其输入的行进行输出。每当我扫描二维码时,应用程序都会将数据放入列中n 1它每次都使用每个可用的下一个空间。我希望数据水平增长。不需要保留样本数据。唯一重要的是正确提取数据。在工作表中输出的样本数据在哪里?您希望数据沿哪一行拆分?我希望数据跨其p行输出ut in.每次扫描二维码时,应用程序都会将数据放在第1列,每次都会使用每个可用的下一个空格。我希望数据水平增长。不需要保留样本数据。唯一重要的是正确提取数据。