COPY AND PASTE TO EACH SHEET BASED ON CELL VALUE

60 views
Skip to first unread message

gspeed book

unread,
Nov 21, 2022, 10:30:51 AM11/21/22
to Google Apps Script Community
Please help me out. i am working on a project that requires me to copy and paste to different sheet base on cell value. for example if I have 3 sheets.The name of the first is man, the name of the second is woman and the name of the third is child. when i type a man in a specific cell let say C7, then code should copy the data sheet D1:j10 to  sheet named man likewise if I type woman in C7, the code should copy D1:J!0 to sheet named woman and if type child into C7  the code should copy D!:J10 to  sheet named child
This is code i have come up with

function trans() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let shtIn = ss.getSheetByName("in");
  let  ssS = SpreadsheetApp.openById("17cRhuKX1jko");
  let shtOut = ssS.getSheetByName("Data");

  let responses = shtIn.getRange("D1::J10").getValues();
  //responses is a 2d array that looks like this [[a],[b],[c]]
  
}



function initSettings_() {
  return [{
    bookId: '17cRhuKDDqjCRFcMKn6b1jko',
    targetSheetName: 'MAN',
    colIndex: 12
  }, {
    bookId: ' 17cRhuKDDqjCRFcMKn6b1jko  ',
    targetSheetName: 'WOMANl',
    colIndex: 13
  }, {
    bookId: ' 17cRhuKDDqjCRFcMKn6b1jko  ',
    targetSheetName: CHILD',
    colIndex: 14
   

  }]
}

function run() {
  const sheet = SpreadsheetApp.getActive()
    .getSheetByName('data')
  const data = sheet.getDataRange().getValues();
  const settings = initSettings_();
  settings.forEach(setts => {
    const pData = prepareData_(data, setts.colIndex);
    if (pData && pData.length)
      SpreadsheetApp.openById(setts.bookId)
        .getSheetByName(setts.targetSheetName)
        .appendRow(pData)
  });
  sheet.getRange("E16:J60").clearContent();
}

/**
 * @param {any[][]} array
 */
function prepareData_(array, colIndex) {
  return array.map(row => row[colIndex])
    .filter(item => item !== '')
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data").getRange("D1:J10").clearContent();
}

Philippe SCHITTER

unread,
Nov 21, 2022, 4:42:04 PM11/21/22
to google-apps-sc...@googlegroups.com
Hello,

In order to execute your code with 4 functions in this same code, try to add this function in "header" : in first position (written in green below).
Then, after this function expCalc() added in green, add after your own code (written in blue below).

function expCalc(){
  trans();
  initSettings_();
  run();
  prepareData_(array, colIndex);
}
Say me if it's good or not ?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/acade8d9-159b-4ff1-a5fc-13333e8ebd02n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages