Home > front end >  Array column duplication
Array column duplication

Time:12-03

How can I convert the data in the following range to the right one with "Google Apps Script"?

[ [ 'A1', 'B1', 'C1' ],
  [ 'A2', 'B2', 'C2' ],
  [ 'A3', 'B3', 'C3' ] ]

to

[ [ 'A1', 'B1', 'C1', 'B1'],
  [ 'A2', 'B2', 'C2', 'B2'],
  [ 'A3', 'B3', 'C3', 'B3'] ]

enter image description here

CodePudding user response:

Try

={A3:C, B3:B}

or depending on your locale

={A3:C \ B3:B}

Change range to suit.

EDIT: if you want a scripted solution try

function addColToArray() {
  const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  const values = sheet.getRange('A4:C').getValues()
    .filter(r => r[0])
    .map((row) => {
      row.push(row[1]) //1 = second column of the array;
      return row;
    })

  console.log(values);

  //write to range E:H
  sheet.getRange(4, 5, values.length, values[0].length).setValues(values);
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Use a simple loop:

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const arr = [ [ 'A1', 'B1', 'C1' ],
  [ 'A2', 'B2', 'C2' ],
  [ 'A3', 'B3', 'C3' ] ];
let i = -1;
while (  i < arr.length) arr[i].push(arr[i][1])
console.log(arr)
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related