Google Sheet - Split Cell contents into rows and columns

后端 未结 2 1556
[愿得一人]
[愿得一人] 2021-02-11 11:56

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:23

    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

提交回复
热议问题