Google Sheet - Split Cell contents into rows and columns

后端 未结 2 2062
一向
一向 2021-02-11 11:31

Linked Google Sheet: https://docs.google.com/spreadsheets/d/1j2P2V0SCiE7QtK7kdCorpLfYQPFRghKMw4tuCYbqce0/edit?usp=sharing

I have a form that outputs all products and qua

2条回答
  •  盖世英雄少女心
    2021-02-11 12:10

    Here is some code that I've tested and it works. The code removes all the words "Product: ", and replaces them with a comma. Then the code does something similar with the string "Quantity: ", replacing it with an empty string. Next it creates an array, and converts the array to a two dimensional array, so that the rows and columns can be written all in one action.

    function convertData() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getSheetByName('Form Submission');
    
      var data = sh.getRange("O2").getValue();
      //Logger.log(data);
      var firstProductRemoved = data.replace("Product: ", "");
      //Logger.log(firstProductRemoved);
    
      var allProductReplacedWithComma = firstProductRemoved.replace(/Product: /g,",");
      //Logger.log(allProductReplacedWithComma);
    
      var allQuantityReplacedWithNothing = allProductReplacedWithComma.replace(/Quantity: /g,"");
      //Logger.log(allQuantityReplacedWithNothing);
    
      var dataAsArray = allQuantityReplacedWithNothing.split(",");
    
      var outerArray = [], innerArray = [];
      var i=0;
    
      for (i=0;i

提交回复
热议问题