最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Combining arrays to use in Google Sheets - Stack Overflow

programmeradmin2浏览0评论

I'm having some trouble bining arrays into one array and then flipping it with the code below. The goal is to set an array of values in one column of a spreadsheet. After the titleArray function, I would like to have an array spread across multiple columns on one row. Then, using a function I found in another Stack Overflow thread (Google Spreadsheet Script - How to Transpose / Rotate Multi-dimensional Array?), I want to flip the array so it is spread across multiple rows on one column. The current code is returning an array for each element in array1 all inside of another array. Then, the transposeArray function is bining values from each array, 1 with 1, 2 with 2, and so on. Can anyone spot what I'm doing wrong here?

var titleArray = function(){
    var output = [];
    for(i=0; i < array1.length; i++){
          if(array1[i].length > 0){   
            var titles = ["",array1[i] + " Data"].concat(array2, array3, array4, array5, array6, array7);
            output.push(titles);
          }
        }
        return(output);
      }
    }

      
    function transposeArray(array){
     var result = [];
     for (var col = 0; col < array[0].length; col++) { // Loop over array cols
       result[col] = [];
        for (var row = 0; row < array.length; row++) { // Loop over array rows
          result[col][row] = array[row][col]; // Rotate
         }
        }
       return result;
      }
    transposeArray(titleArray);

I'm having some trouble bining arrays into one array and then flipping it with the code below. The goal is to set an array of values in one column of a spreadsheet. After the titleArray function, I would like to have an array spread across multiple columns on one row. Then, using a function I found in another Stack Overflow thread (Google Spreadsheet Script - How to Transpose / Rotate Multi-dimensional Array?), I want to flip the array so it is spread across multiple rows on one column. The current code is returning an array for each element in array1 all inside of another array. Then, the transposeArray function is bining values from each array, 1 with 1, 2 with 2, and so on. Can anyone spot what I'm doing wrong here?

var titleArray = function(){
    var output = [];
    for(i=0; i < array1.length; i++){
          if(array1[i].length > 0){   
            var titles = ["",array1[i] + " Data"].concat(array2, array3, array4, array5, array6, array7);
            output.push(titles);
          }
        }
        return(output);
      }
    }

      
    function transposeArray(array){
     var result = [];
     for (var col = 0; col < array[0].length; col++) { // Loop over array cols
       result[col] = [];
        for (var row = 0; row < array.length; row++) { // Loop over array rows
          result[col][row] = array[row][col]; // Rotate
         }
        }
       return result;
      }
    transposeArray(titleArray);

Share Improve this question edited May 23, 2017 at 12:12 CommunityBot 11 silver badge asked Nov 21, 2014 at 14:07 Calvin GaunceCalvin Gaunce 3661 gold badge4 silver badges19 bronze badges 2
  • What happens with last line change to transposeArray(titleArray());? – Martin Ernst Commented Nov 21, 2014 at 14:31
  • Each value is paired with corresponding values in the other array. After the first function, I see something like this... [[,array1value1,array1value2,array1value3],[array2value1,array2value2,array2value3]] Then after the second function, I see something like this... [[, ],[array1value1,array2value1],[array1value2,array2value2],[array1value3,array2value3]] – Calvin Gaunce Commented Nov 21, 2014 at 14:36
Add a ment  | 

1 Answer 1

Reset to default 5

I was able to sit down with a programmer and he helped me through this. Essentially, I needed to use the concat method instead of the push method. I tried using concat early in this process but was using it in the first way demonstrated below instead of the second. I've included the full new code below as well.

output.concat(titles);
output = output.concat(titles);

var titleArray = function(){
    var output = [];
    for(i=0; i < array1.length; i++){
          if(array1[i].length > 0){   
            var titles = ["",array1[i] + " Data"].concat(array2, array3, array4, array5, array6, array7);
            ouput = output.concat(titles);
          }
        }
        return(output);
      }
    }

      
    function transposeArray(array){
     var result = [];
     for (var col = 0; col < array[0].length; col++) { // Loop over array cols
       result[col] = [];
        for (var row = 0; row < array.length; row++) { // Loop over array rows
          result[col][row] = array[row][col]; // Rotate
         }
        }
       return result;
      }
    transposeArray([titleArray]);

发布评论

评论列表(0)

  1. 暂无评论