Search code examples
google-apps-scriptgoogle-sheetsduplicates

How to copy the corresponding data of the duplicate cells with each other in google sheets script?


Column a has duplicate data with the corresponding state in column B and I want to duplicate the state of Column B for other duplicates. for example column A has data as "2307PT715" and column B as: "Processing". I need to duplicate this status: "Processing" for all duplicate data with "2307PT715". Same as above: 2407PT681 will also have "done" status.

Thank you everyone for your help.

Column A Column B
2307PT344 Blank
2307PT344
2307PT850
2307PT715 Processing
2307PT715
2407PT681 Done
2407PT681
2307PT733
2307PT733
2307PT850
2307PT715
2307PT850
2307PT866
2307PT866
2307PT866
2307PT344

Here's the result that I want.

Column A Column B
2307PT344 Blank
2307PT344 Blank
2307PT850
2307PT715 Processing
2307PT715 Processing
2407PT681 Done
2407PT681 Done
2307PT733
2307PT733
2307PT850
2307PT715 Processing
2307PT850
2307PT866
2307PT866
2307PT866
2307PT344 Blank

Solution

  • Although I'm not sure whether I could correctly understand your question, how about the following sample script of Google Apps Script?

    Sample script:

    Please copy and paste the following script to the script editor of Google Spreadsheet and set the sheet name and save the script.

    function myFunction() {
      const sheetName = "Sheet1"; // Please set your sheet name.
    
      // Retrieve values from the sheet in Spreadsheet.
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      const range = sheet.getRange("A1:B" + sheet.getLastRow());
      const values = range.getValues();
    
      // Create an object for searching the values of column "B" using the values of column "A".
      // Here, an object like {"2307PT344": "Blank", "2307PT715": "Processing",,,} is created.
      const obj = values.reduce((o, [a, b]) => {
        if (b.toString()) o[a] = b;
        return o;
      }, {});
      
      // Creating the destination values using the object.
      // Here, 2-dimentional array like [["Blank"],["Blank"],,,] is created.
      const dstValues = values.map(([a]) => [obj[a] || null]);
      
      // Put the destination values in column "B".
      range.offset(0, 1, dstValues.length, 1).setValues(dstValues);
    }
    

    Testing:

    When this script is run to your provided sample table, the following result is obtained.

    enter image description here

    Note:

    • When your actual Spreadsheet has the 1st header row, please modify "A1:B" to "A2:B".

    References:

    Added:

    From your reply,

    Your help is very helpful and I have applied very well. however i tried applying it to the expansion, when using data in column B and status in column F or any other column the file doesn't work properly. Can you help me adjust it? Your help is very helpful and I have applied very well. however i tried applying it to the expansion, when using data in column B and status in column F or any other column the file doesn't work properly. Can you help me adjust it?

    I thought that from the tables of your question, the tables have 2 columns "A" and "B". But, it seems that those columns were "B" and "F". And also, I couldn't correctly imagine your current script from your reply. So, for this situation, I updated the above script. Please confirm it.

    function myFunction() {
      const sheetName = "Sheet1"; // Please set your sheet name.
    
      // Retrieve values from the sheet in Spreadsheet.
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      const range = sheet.getRange("B1:F" + sheet.getLastRow());
      const values = range.getValues();
    
      // Create an object for searching the values of column "F" using the values of column "B".
      // Here, an object like {"2307PT344": "Blank", "2307PT715": "Processing",,,} is created.
      const obj = values.reduce((o, [b, , , , f]) => {
        if (f.toString()) o[b] = f;
        return o;
      }, {});
    
      // Creating the destination values using the object.
      // Here, 2-dimentional array like [["Blank"],["Blank"],,,] is created.
      const dstValues = values.map(([b]) => [obj[b] || null]);
    
      // Put the destination values in column "F".
      range.offset(0, 4, dstValues.length, 1).setValues(dstValues);
    }