skip to Main Content

How to make .map() ignore values in array. In script below values in column B and C of dstSheet are erased when running script. I want to keep it. How to modify this script?

function dorozliczenia() {
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("arkusz 1");
  const dstSS = SpreadsheetApp.openById("1bjqauqiyEzUkECaxqswRmOQPYWydpSAvLf97t5ovbXQ");
  const dstSheet = dstSS.getSheetByName("arkusz 1");

  const obj = Object.fromEntries(srcSheet.getRange("A2:C" + srcSheet.getLastRow()).getValues().map(([a, , c]) => [c, a]));
  const dstRange = dstSheet.getRange("A2:D" + dstSheet.getLastRow());
  const values = dstRange.getValues().map(([, , , a]) => [obj[a] ? true : false,null ,null , a]);
  dstRange.setValues(values);
}

Here are the spreadsheets:

https://docs.google.com/spreadsheets/d/1bjqauqiyEzUkECaxqswRmOQPYWydpSAvLf97t5ovbXQ/ https://docs.google.com/spreadsheets/d/11ZVtFJ5Ul5nUu3PLxUu_xBlfS0bbeAWRj8-ZKwKUTG8/

2

Answers


  1. In your script, how about the following modification?

    From:

    const values = dstRange.getValues().map(([, , , a]) => [obj[a] ? true : false,null ,null , a]);
    

    To:

    const values = dstRange.getValues().map(([, b, c, d]) => [obj[d] ? true : false, b, c, d]);
    
    • I thought that the reason for your current issue might be due to null ,null of [obj[a] ? true : false,null ,null , a]. So, the current value is used as this modification.

    • Or, how about putting only the values of column "A" as follows?

        const values = dstRange.getValues().map(([, , , a]) => [obj[a] ? true : false]);
        dstRange.offset(0, 0, values.length, 1).setValues(values);
      
    Login or Signup to reply.
  2. To modify the script to keep the values in columns B and C of the dstSheet when running the script, you can adjust the code that creates the values array. Currently, the script is only creating a new array with a boolean value and the value from column A for each row in the dstSheet, effectively erasing the values in columns B and C.

    To keep the values in columns B and C, you can modify the code that creates the values array to include the existing values from those columns.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search