copyto columns + added

361 views
Skip to first unread message

ShiizophreN

unread,
Jul 23, 2022, 1:11:03 AM7/23/22
to Google Apps Script Community
Hi dear friends, I have a problem when implementing a script. I have a table column with 80 rows, I would like to copy the values into a log table and when copying again using onEdit, the values should be added to the first copy

I hope for your help and thank you all
Screenshot_3.png

Clark Lind

unread,
Jul 23, 2022, 8:30:34 AM7/23/22
to Google Apps Script Community
So you will manually trigger a copy function (or trigger on form submit maybe?) which copies one column to another Sheet (or Spreadsheet) [the 'Log'], and then when the Log is edited, you want to put the edited value back in the first sheet? Or something else? Can you clarify a little to ensure my understanding please?

ShiizophreN

unread,
Jul 23, 2022, 12:39:15 PM7/23/22
to Google Apps Script Community
Hallo erst einmal vielen dank für die angebotene hilfe, also ich möchte die spalte in ein anderes tabellenblatt kopieren und immer wenn ich die spalte kopiere möchte ich das sich die zellen summieren

ShiizophreN

unread,
Jul 24, 2022, 12:14:41 AM7/24/22
to Google Apps Script Community
First Trigger with time trigger on midnight
1.png

second trigger with time trigger on midnight
2.png

next time trigger with time trigger on midnight
3.png

Clark Lind

unread,
Jul 24, 2022, 9:03:43 AM7/24/22
to Google Apps Script Community
Ok, that makes better sense now. It should be pretty straight forward, and there may be more advanced ways and better ways to do this, but this is how I would do it:
1. Get data column values as an array (array1)
2. Get destination column range.
3. Get destination column values as an array (array2). 
4. For each value in array1, add that value to the same index in array2 and store in new array (array3).
5. Overwrite values in destination column with array3 values.

Let me see what I can come up with.

Clark Lind

unread,
Jul 24, 2022, 9:19:31 AM7/24/22
to Google Apps Script Community
Ok, this seems to work as an example. Change the sheetnames and ranges to your names and ranges.

function addArrays() {
  //helper function to 'flatten' a 2-d array into 1-d array (e.g., from array[0][5] to array[5])
  //receives 2-d array and returns 1-d array
  const flat = (arr) => arr.reduce((a, b) => (Array.isArray(b) ? [...a, ...flat(b)] : [...a, b]), []);

//1. Get data column values as an array (array1)
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("ArrayCombineSource");
  var data = ws.getRange("A1:A12").getValues();
  //flatten array
  data = flat(data);

// 2. Get destination column range.
  var destination = ss.getSheetByName("ArrayCombineTarget").getRange("C1:C12");

// 3. Get destination column values as an array (array2). 
  var destValues = destination.getValues();
  //flatten array
  destValues = flat(destValues);

// 4. For each value in array1, add that value to the same index in array2 and store in new array (array3).
  array3 = [];  //declare new array to hold new values
  data.forEach( (num, idx) => {
      array3.push([num + destValues[idx]])
  })

// 5. Overwrite values in destination column with array3 values.
destination.setValues(array3)
  
}

ShiizophreN

unread,
Jul 24, 2022, 10:22:00 AM7/24/22
to Google Apps Script Community
Woow thank you sooo much for your help :D it works perfect for me
Reply all
Reply to author
Forward
0 new messages