I'd like to copy ranges "A1", "C1:Z1", "A3", "C3:Z3", "A6", "C6:Z6" from source sheet and paste the values of those cells to the "target" sheet while eliminating the non-selected cells (such as column "B", row2, row4 and 5.
Source
Target
Use getRange() to retrieve all of the range and exclude col2, row 2, 4 and 5 using Array.filter
const values = sourceSheet.getDataRange().getValues();
const rowIndexToArrayIndex = num => num - 1;
const excludeCols = [2].map(rowIndexToArrayIndex);
const excludeRows = [2, 4, 5].map(rowIndexToArrayIndex);
const filteredValues = values
.filter((_row, i) => !excludeRows.includes(i))
.map(row => row.filter((_, j) => !excludeCols.includes(j)));
console.info({ values, filteredValues });
//MOCK values
const sourceSheet = {
getDataRange: () => ({
getValues: () =>
[...new Array(5)].map(
(i => () => new Array(10).fill().map(() => ++i))(0)
),
}),
};
//MOCK ends
const values = sourceSheet.getDataRange().getValues();
const rowIndexToArrayIndex = num => num - 1;
const excludeCols = [2].map(rowIndexToArrayIndex);
const excludeRows = [2, 4, 5].map(rowIndexToArrayIndex);
const filteredValues = values
.filter((_row, i) => !excludeRows.includes(i))
.map(row => row.filter((_, j) => !excludeCols.includes(j)));
console.info({ values, filteredValues });
I believe your goal as follows.
You want to copy the values of cells with the highlighted background color from the source sheet to the target sheet using Google Apps Script.
Flow:
Retrieve the background colors and values from the source sheet.
Create an array for copying to the target sheet using the retrieved background colors and values.
Put the created array to the target sheet.
Sample script 1:
In this sample, from your sample input and output iamges, the values of the cells with highlighted background color are retrieved. Before you use this script, please set the variables of sourceSheetName, destinationSheetName, backgroundColor.
function myFunction() {
var sourceSheetName = "Sheet1"; // Please set the source sheet name.
var destinationSheetName = "Sheet2"; // Please set the destination sheet name.
var backgroundColor = "###"; // Please set the background color you want to check.
// 1. Retrieve the background colors and values from the source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName(sourceSheetName);
var range = srcSheet.getDataRange();
var backgroundColors = range.getBackgrounds();
var values = range.getValues();
// 2. Create an array for copying to the target sheet using the retrieved background colors and values.
var copyValues = backgroundColors.reduce((ar1, r, i) => {
var temp = r.reduce((ar2, c, j) => {
if (c == backgroundColor) ar2.push(values[i][j]);
return ar2;
}, []);
if (temp.length > 0) ar1.push(temp);
return ar1;
}, []);
// 3. Put the created array to the target sheet.
var dstSheet = ss.getSheetByName(destinationSheetName);
dstSheet.getRange(1, 1, copyValues.length, copyValues[0].length).setValues(copyValues);
}
Sample script 2:
In this pattern, in your sample input situation, for example, even when the cells "C3" and "A6" are the default background color, the script copies the values from the highlighted cells by removing the columns with the default background color.
function myFunction() {
var sourceSheetName = "Sheet1"; // Please set the source sheet name.
var destinationSheetName = "Sheet2"; // Please set the destination sheet name.
var backgroundColor = "###"; // Please set the background color you want to check.
// 1. Retrieve the background colors and values from the source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName(sourceSheetName);
var range = srcSheet.getDataRange();
var backgroundColors = range.getBackgrounds();
var values = range.getValues();
// 2. Create an array for copying to the target sheet using the retrieved background colors and values.
var tempValues = backgroundColors.reduce((ar1, r, i) => {
var temp = r.reduce((ar2, c, j) => {
ar2[j] = c == backgroundColor ? values[i][j] : "";
return ar2;
}, []);
if (temp.length > 0 && temp.some(e => e.toString() != "")) {
ar1.push(temp);
}
return ar1;
}, []);
var obj = tempValues.reduce((o, r, i) => {
r.forEach((c, j) => {
if (c.toString() == "" && i == 0) o[j] = true;
if (c.toString() != "" && o[j] && i > 0) delete o[j];
});
return o;
}, {});
var copyValues = tempValues.map(r => r.filter((_, j) => !obj[j]));
// 3. Put the created array to the target sheet.
var dstSheet = ss.getSheetByName(destinationSheetName);
dstSheet.getRange(1, 1, copyValues.length, copyValues[0].length).setValues(copyValues);
}
Note:
In your sample script, I prepared from your input situation. So when your actual situation is different from the sample situation, the script might not work. So please be careful this.
If you want to copy the values of cells except for the default background color #ffffff, please modify above script as follows.
From
if (c == backgroundColor) ar2.push(values[i][j]);
To:
if (c != "#ffffff") ar2.push(values[i][j]);
or
From
ar2[j] = c == backgroundColor ? values[i][j] : "";
To:
ar2[j] = c != "#ffffff" ? values[i][j] : "";
When the Spreadsheet of the source sheet and the taget sheet is different, please modify var ss = SpreadsheetApp.getActiveSpreadsheet().
References:
getBackgrounds()
getValues()
reduce()
Related
I was able to filter out the displayed data on first load for a particular user by searching a column by using Tanaike's method in this thread. The search functions works fine but the thing is, the user can also search for other people's record in the google sheet as long as they search for a value existing in the spreadsheet. I am thinking of a way to add a second && condition to show only the rows that only matches that currentUser's username (in column C) and the searched value.
function displayOwnRecordSearch(currentUser, searchedValue){
var spreadsheetId = "";
var sheetName = "";
var column = 3;
var [, ...data] = Sheets.Spreadsheets.Values.get(spreadsheetId, sheetName).values;
var arr = [];
if(searchedValue !== undefined) {
const validateText = (query) => {
let regex = new RegExp(searchedValue, 'i')
return regex.test(query)
}
data.forEach(d => {
if (validateText(d)) {
arr.push(d);
}
});
}else{
const validateText = (query) => {
let regex = new RegExp(currentUser, 'i')
return regex.test(query)
}
data.forEach(d => {
if (validateText(d[column - 1])) {
arr.push(d);
}
});
}
return arr;
}
In your situation, how about using fiter as follows?
Modified script:
function displayOwnRecordSearch(currentUser, searchedValue) {
var spreadsheetId = "###";
var sheetName = "###";
var columnForCurrentUser = 3; // Column "C".
var columnForSearchedValue = 5; // Column "E". This is from https://stackoverflow.com/q/75066197 (your previous question)
var [, ...data] = Sheets.Spreadsheets.Values.get(spreadsheetId, sheetName).values;
var arr = data.filter(r => (new RegExp(currentUser, 'i')).test(r[columnForCurrentUser - 1]) && (new RegExp(searchedValue, 'i')).test(r[columnForSearchedValue - 1]));
// console.log(arr); // You can check the value in the log.
return arr;
}
In this modification, From your previous question, I used var columnForSearchedValue = 5; as the column for searchedValue.
Or, I think that var [, ...data] = Sheets.Spreadsheets.Values.get(spreadsheetId, sheetName).values; can be also modified as follows.
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
Reference:
filter()
I need to create a script which deletes all empty rows and columns (with no value in any cell of the row/column) from indicated sheet starting from 1 column/row, at the same time - using batch update. I have found a script here and tried to suit it to my need.
I have modified it like below, but I do something wrong with function arguments (and probably something else).
function clear(){
const sheetName = "Parser"; // Please set the sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName(sheetName);
const sheetId = sh.getSheetValues;
var values = sh.getRange('B1:B').getValues();
var requests = values.reduce((ar, value) => {
var maxColumns = sh.getMaxColumns();
var lastColumn = sh.getLastColumn();
var maxRows = sh.getMaxRows();
var lastRow = sh.getLastRow();
if (lastRow == 0 && lastColumn == 0 && maxRows > 1 && maxColumns > 1) {
ar.push({deleteDimension: {range: {sheetId: sheetId, dimension: "ROWS", startIndex: 1}}});
ar.push({deleteDimension: {range: {sheetId: sheetId, dimension: "COLUMNS", startIndex: 1}}});
}
Logger.log(ar);
return ar;
}, []);
if (requests.length > 0) {
Sheets.Spreadsheets.batchUpdate({requests: requests}, id);
}
};
Begging for help!
From:
to:
Try:
function clean() {
const sheetName = "Parser"
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
const cleanRows = sheet.getDataRange()
.getValues()
.filter(row => !row.every(cell => cell === ``))
const cleanCols = cleanRows[0].map((_, index) => cleanRows.flatMap(row => row[index]))
.filter(col => !col.every(cell => cell === ``))
const values = cleanCols[0].map((_, index) => cleanCols.flatMap(row => row[index]))
sheet.getDataRange().clearContent()
sheet.getRange(1, 1, values.length, values[0].length).setValues(values)
if (sheet.getLastRow() !== sheet.getMaxRows()) sheet.deleteRows(sheet.getLastRow()+1, sheet.getMaxRows()-sheet.getLastRow())
if (sheet.getLastColumn() !== sheet.getMaxColumns()) sheet.deleteColumns(sheet.getLastColumn()+1, sheet.getMaxColumns()-sheet.getLastColumn())
}
This will filter out all empty rows, rotate the array, filter out all empty 'columns', then rotate the array back and update the sheet.
function clear() {
const sheetName = "Parser"
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).clearContent()
}
Let me know if this works for you!
Clean() Result:
Good day folks! I have this codes in which I want to combine into one, but having trouble to do so.
This is the first code in which the data is copied from source sheet to target sheet added to the after the last row with data
function DE() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); // activates the opened document
let sourceSheet = spreadSheet.getSheetByName('Support Sheet'); // selects the sheet where the data will be coming from
let sourceSheet2 = spreadSheet.getSheetByName('Data Entry'); // selects the sheet where the sheet name contains
let sourceRange = sourceSheet.getDataRange();
let sourceRange2 = sourceSheet2.getDataRange();
let sourceValues = sourceRange.getValues();
let sourceValues2 = sourceRange2.getValues();
let sheetName = sourceValues2[1][1];
sourceValues = sourceValues.slice(1).map(row => row.slice(13,13+10));
let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;
let targetSheet = spreadSheet.getSheetByName(sheetName);
let lastRow = targetSheet.getLastRow() + 1;
let targetRange = targetSheet.getRange(lastRow,1,rowCount,columnCount);
targetRange.setValues(sourceValues);
}
And this is the code that I saw here which works perfectly in my spreadsheet, where this one updates the column B if there were changes based on unique ID (column A)
function updateEntrees() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Support Sheet');
var rg1a=sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a=rg1a.getValues();
var rg1b=sh1.getRange(2,2,sh1.getLastRow()-1,1);
var vA1b=rg1b.getValues();
var sh2=ss.getSheetByName('Target Sheet');
var rg2a=sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a=rg2a.getValues();
var rg2b=sh2.getRange(2,2,sh2.getLastRow()-1,1);
var vA2b=rg2b.getValues();
for(var i=0;i<vA1a.length;i++) {
for(var j=0;j<vA2a.length;j++) {
if(vA1a[i][0]==vA2a[j][0]) {
vA2b[j][0]=vA1b[i][0]
}
}
}
rg2b.setValues(vA2b);
}
Now I am wondering how I am going to combine this 2, where if the source sheet has unique ID that needs updating it will update the target sheet and if there is a new unique ID, it will just add the data at the bottom
I believe your goal is as follows.
You have 2 sheets of the source sheet and the target sheet.
You want to update and append values from the source sheet to the target sheet by checking the column "A" of both sheets.
In this case, how about the following modified script?
Modified script:
function myFunction() {
// 1. Retrieve values from the source and target sheets.
var ss = SpreadsheetApp.getActive();
var [srcSheet, targetSheet] = ['Support Sheet', 'Target Sheet'].map(s => ss.getSheetByName(s));
var [srcValues, targetValues] = [srcSheet, targetSheet].map(s => s.getLastRow() == 1 ? [] : s.getRange("A2:B" + s.getLastRow()).getValues());
// 2. Create objects for searching values of the column "A".
var [srcObj, targetObj] = [srcValues, targetValues].map(e => e.reduce((o, [a, b]) => (o[a] = b, o), {}));
// 3. Check update values at the target sheet.
var updatedValues = targetValues.map(([a, b]) => [a, (srcObj[a] || (b || ""))]);
// 4. Check append values.
var appendValues = srcValues.reduce((ar, [a, b]) => {
if (!targetObj[a]) ar.push([a, b]);
return ar;
}, []);
// 5. Update the target sheet.
var values = [...updatedValues, ...appendValues];
targetSheet.getRange(2, 1, values.length, values[0].length).setValues(values);
}
From your script, this sample script supposes that your 1st row of both sheets is the header row. Please be careful about this.
Note:
I proposed the above script by guessing your Spreadsheet from your script and question. When this script is not useful for your situation, can you provide the sample Spreadsheet? By this, I would like to confirm it.
References:
reduce()
map()
Added 1:
From the following replying,
this works fine, but the data being added to the target sheet is just column A and B, here is the sample sheet: docs.google.com/spreadsheets/d/… where the range from source sheet that needs to transfer to target sheet is N:X
How about the following sample script?
Modified script:
function myFunction() {
// 1. Retrieve values from the source and target sheets.
var ss = SpreadsheetApp.getActive();
var [srcSheet, targetSheet] = ['Source Sheet', 'Target Sheet'].map(s => ss.getSheetByName(s));
var [srcValues, targetValues] = [[srcSheet, "N2:X"], [targetSheet, "A2:K"]].map(s => s[0].getLastRow() == 1 ? [] : s[0].getRange(s[1] + s[0].getLastRow()).getValues());
// 2. Create objects for searching values of the column "A".
var [srcObj, targetObj] = [srcValues, targetValues].map(e => e.reduce((o, [a, ...b]) => (o[a] = b, o), {}));
// 3. Check update values at the target sheet.
var updatedValues = targetValues.map(([a, ...b]) => [a, ...(srcObj[a] || b)]);
// 4. Check append values.
var appendValues = srcValues.reduce((ar, [a, ...b]) => {
if (!targetObj[a]) ar.push([a, ...b]);
return ar;
}, []);
// 5. Update the target sheet.
var values = [...updatedValues, ...appendValues];
targetSheet.getRange(2, 1, values.length, values[0].length).setValues(values);
}
Added 2:
About your following new question,
what if I just want to update the Column B in target sheet? and other column will stay the same?
How about the following script?
Modified script:
function myFunction() {
// 1. Retrieve values from the source and target sheets.
var ss = SpreadsheetApp.getActive();
var [srcSheet, targetSheet] = ['Source Sheet', 'Target Sheet'].map(s => ss.getSheetByName(s));
var [srcValues, targetValues] = [[srcSheet, "N2:X"], [targetSheet, "A2:K"]].map(s => s[0].getLastRow() == 1 ? [] : s[0].getRange(s[1] + s[0].getLastRow()).getValues());
// 2. Create objects for searching values of the column "A".
var [srcObj, targetObj] = [srcValues, targetValues].map(e => e.reduce((o, [a, ...b]) => (o[a] = b, o), {}));
// 3. Check update values at the target sheet.
var updatedValues = targetValues.map(([a, b]) => [a, (srcObj[a] || (b || ""))]);
// 4. Check append values.
var appendValues = srcValues.reduce((ar, [a, ...b]) => {
if (!targetObj[a]) ar.push([a, ...b]);
return ar;
}, []);
// 5. Update the target sheet.
targetSheet.getRange(2, 1, updatedValues.length, updatedValues[0].length).setValues(updatedValues);
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, appendValues.length, appendValues[0].length).setValues(appendValues);
}
I have a function that change the background color of cells.
Function works well and do the thing I want, but I met one problem that I don't really know how to solve.
I want this function to loop through all used columns in spreadsheet. (for now it is from G till TP column will increase)
As you can see the function I have now do the thing only with G column.
How to make it loop till the last used column?
function insertColor2() {
const sheetName = "結果1"; // set the sheet name.
// 1. Retrieve values from sheet.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const values1 = sheet.getRange(3, 7, sheet.getLastRow() - 2,1).getValues();
const values = [];
values.push(values1);
// 2. Create an array for modifying the background colors.
const backgroundColors = values.map(([,,c,,,...g]) =>
g.reduce((o, e) => {
if (e.toString() != "") {
o.total += e;
o.colors.push(c >= o.total ? null : "red");
} else {
o.colors.push(null);
}
return o;
}, {colors: [], total: 0}).colors
);
const flatten = [].concat.apply([], backgroundColors);
const newArr = [];
while(flatten.length) newArr.push(flatten.splice(0,1));
Logger.log(newArr);
// 3. Modify the background colors of cells.
sheet.getRange(8, 7, newArr.length, 1).setBackgrounds(newArr);
}
So I found a solution to solve this problem. It is not the best solution but works well. I just simply transposed array twice at the beginning and before inputting result to the sheet.
function transpose(a) {
return Object.keys(a[0]).map(function(c) {
return a.map(function(r) { return r[c]; });
});
}
function transpose1(original) {
var copy = [];
for (var i = 0; i < original.length; ++i) {
for (var j = 0; j < original[i].length; ++j) {
// skip undefined values to preserve sparse array
if (original[i][j] === undefined) continue;
// create row if it doesn't exist yet
if (copy[j] === undefined) copy[j] = [];
// swap the x and y coords for the copy
copy[j][i] = original[i][j];
}
}
return copy;
}
function insertColor5() {
const sheetName = "結果1"; // Please set the sheet name.
// 1. Retrieve values from sheet.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const values1 = sheet.getRange(3, 7, sheet.getLastRow() - 2, sheet.getLastColumn()).getValues();
const values = transpose(values1);
//Logger.log(values);
// 2. Create an array for modifying the background colors.
const backgroundColors = values.map(([,,c,,,...g]) =>
g.reduce((o, e) => {
if (e.toString() != "") {
o.total += e;
o.colors.push(c >= o.total ? null : "red");
} else {
o.colors.push(null);
}
return o;
}, {colors: [], total: 0}).colors
);
const kolorki = transpose1(backgroundColors);
//Logger.log(transpose1(backgroundColors));
// 3. Modify the background colors of cells.
sheet.getRange(8, 7, kolorki.length, kolorki[0].length).setBackgrounds(kolorki);
}
I have two spreadsheet books.
BookA
BookB
I have sheet names of BookB stored in BookA.
I want to search through all the sheets in BookB that matches the sheet name stored in BookA. If a sheet is found get the values in Cell 'A3' and paste it in BookA in front of the respective sheet name.
(I have managed to achieve this task successfully. Issue comes now. Brace yourselves)
I want to get the 'File Format' details without duplicates from the sheets of BookB and paste that in the sheet of BookA in front of the page name. May be my way is not correct. If someone can help I am grateful.
Note that File Format details are mentioned in two different ranges in the given two sheets. ALBW - D6:D21 and BFLCB - F6:F21
const pmsRange = 'A3' // the cell in book B sheet i that you want to copy
function getFileFormat(){
const ssA = SpreadsheetApp.openById(bookAId);
const sA = ssA.getSheetByName(sheetA);
const sheetNames = sA.getRange('G2:G').getValues().reduce((names, row) => row[0] !== '' ? names.concat(row[0]) : names ,[]);
const ssB = SpreadsheetApp.openById(bookBId);
const valuesFromSheetB = []; // collect the values you find in each sheet of book B
for (const sheetName of sheetNames) {
const sheet = ssB.getSheetByName(sheetName);
if (!sheet) {
valuesFromSheetB.push(['Sheet Not Found']);
continue;
}
const value = sheet.getRange(pmsRange).getValue(); // get the value from the range you specified
var array1 = [{}];
var string1 = value;
array1 = string1.split(/[:\n]/);
var pms = array1[1];
pms = pms.replace(/\s+/g, '');
if(pms.toLowerCase()=="onq"){
console.log(sheetName+":"+pms);
var col0 = exts.map(function(value,index) { return value[0]; });
const distinct = (value, index, self) =>{ return self.indexOf(value)===index;}
var unq = col0.filter(distinct).toString();
console.log(unq)
extsFromSheetB.push([unq])
}
sA.getRange(2, 8, valuesFromSheetB.length, 1).setValues(valuesFromSheetB); // paste all of the values you collected into the paste range you specified in book A
}
}
These edits should get you what you need. The script will find sheets in book B whose names are listed in book A. Once a sheet is found, it will check to see if the value in the pmsRange of that sheet contains the pmsSearchValue. If it does, then it will store all of the file formats separated by ' / '. If it doesn't then it will store ''. Finally, after iterating over every sheet name collected from book A, it will paste the file formats into the paste range that you specified in your example.
const pmsRange = 'A3' // the cell in book B sheet i that you want to copy
const pmsSearchValue = 'OnQ';
const fileFormatCol = 4 // column D
const fileFormatRow = 6 // first row containing file formats
function getFileFormat(){
const ssA = SpreadsheetApp.openById(bookAId);
const sA = ssA.getSheetByName(sheetA);
const sheetNames = sA.getRange('G2:G').getValues().reduce((names, row) => row[0] !== '' ? names.concat(row[0]) : names ,[]);
const ssB = SpreadsheetApp.openById(bookBId);
const fileFormatsFromBookB = []; // collect the values you find in each sheet of book B
for (const sheetName of sheetNames) {
const sheet = ssB.getSheetByName(sheetName);
if (!sheet) continue;
const pmsCell = sheet.getRange(pmsRange).getValue();
if (pmsCell && pmsCell.indexOf(pmsSearchValue)) {
const fileFormatRange = sheet.getRange(fileFormatRow, fileFormatCol, sheet.getLastRow(), 1);
const fileFormats = fileFormatRange.getValues().filter(f => f !== '').join(' / ');
fileFormatsFromBookB.push([fileFormats]);
} else {
fileFormatsFromBookB.push(['']);
}
sA.getRange(2, 10, fileFormatsFromBookB.length, 1).setValues(fileFormatsFromBookB); // paste all of the values you collected into the paste range you specified in book A
}
}
References: None. This is mostly vanilla javascript taking advantage of the Apps Script Spreadsheet Class that you are already using in the sample in your question.
I managed to get the code edited by #RayGun to to fit to my requirement. Thank you. Posting the code here if someone else face the same issue as me.
const pmsRange = 'A3' // the cell in book B sheet i that you want to copy - pms
const pmsOnq = 'onq';
const pmsFosse = 'fosse'
const pmsGalaxy = 'galaxylightspeed'
const pmsOpera = 'opera'
const fileFormatCol = 4 // column D
const fileFormatRow = 6 // first row containing file formats
const operaCol = 6 // column F
const operaRow = 6 // first row of opera file formats
function getFileFormat(){
const ssA = SpreadsheetApp.openById(bookAId);
const sA = ssA.getSheetByName(sheetA);
const sheetNames = sA.getRange('G2:G').getValues().reduce((names, row) => row[0] !== '' ? names.concat(row[0]) : names ,[]);
const ssB = SpreadsheetApp.openById(bookBId);
const fileFormatsFromBookB = []; // collect the values you find in each sheet of book B
for (const sheetName of sheetNames) {
const sheet = ssB.getSheetByName(sheetName);
if (!sheet){
fileFormatsFromBookB.push(['Sheet Not Found'])
continue;
}
const pmsCell = sheet.getRange(pmsRange).getValue();
var array1 = [{}];
var string2 = pmsCell;
array1 = string2.split(/[:\n]/);
var pms = array1[1];
pms = pms.replace(/\s+/g, '').toLowerCase();
console.log(sheetName)
console.log(pms)
if (pms==pmsOnq || pms==pmsFosse || pms==pmsGalaxy) {
const fileFormatRange = sheet.getRange(fileFormatRow, fileFormatCol, sheet.getLastRow(), 1);
const fileFormats = fileFormatRange.getValues();
var col0 = fileFormats.map(function(value,index) { return value[0]; });
const distinct = (value, index, self) =>{ return self.indexOf(value)===index;}
var unq = col0.filter(distinct).toString();
fileFormatsFromBookB.push([unq]);
}
if(pms==pmsOpera){
const fileFormatRange = sheet.getRange(operaRow, operaCol, sheet.getLastRow(), 1);
const fileFormats = fileFormatRange.getValues();
var col0 = fileFormats.map(function(value,index) { return value[0]; });
const distinct = (value, index, self) =>{ return self.indexOf(value)===index;}
var unq = col0.filter(distinct).toString();
fileFormatsFromBookB.push([unq]);
}
/*else {
fileFormatsFromBookB.push(['']);
}*/
sA.getRange(2, 10, fileFormatsFromBookB.length, 1).setValues(fileFormatsFromBookB); // paste all of the values you collected into the paste range you specified in book
}
}
Note that you have to remove that else part of if statement. Otherwise it skips a cell and gives a result in a wrong range.