I want To create One object in which I want to store Cell address and there column and row index. I have achieve that using this code but in mereged cell I'm facing issue. If A1 and B1 Cell is merged than I want only A1 Cell but currently I get both A1 and B1 cell. Is there way I can find merged cell from given range(namedRange).
HERE is my Image My Excel Image According to this table I want only A1 and G1 cell from given range because other cells are merge.
Here
async function setDefineNameInExcel(context, sheetName, SheetRange, createRangeName) {
var uniqueNamedRange = createRangeName;
let sheet = context.workbook.worksheets.getItem(sheetName);
let range = sheet.getRange(SheetRange);
sheet.names.add(uniqueNamedRange, range);
range.load("address,rowCount,columnCount,cellCount,values");
sheet.load("names");
await context.sync();
const propertiesToGet = range.getCellProperties({ address: true });
await context.sync();
for (let iRow = 0; iRow < range.rowCount; iRow++) {
for (let iCol = 0; iCol < range.columnCount; iCol++) {
const cellAddress = propertiesToGet.value[iRow][iCol];
var address = cellAddress.address.slice(cellAddress.address.lastIndexOf("!") + 1)
excelTableObj = {};
excelTableObj.Range = uniqueNamedRange;
excelTableObj.Address = address;
excelTableObj.RowIndex = iRow;
excelTableObj.ColIndex = iCol;
lstexcelTableObj.push(excelTableObj);
}
}
}
I have this function I use to get an Array of Objects of all "Merged Cells". You could use the below function first, then you'd cross reference in the loop in your function to see if a cell was part of a "Merged Area" and only include the first cell. I leave that to you to integrate, hopefully the below can get you on the right track..
async function Get_Merged_Areas_Arr_Of_Objs(context, ws) {
var mergedAreas = ws.getUsedRange(true).getMergedAreasOrNullObject();
mergedAreas.load("areas");
await context.sync();
var Merged_Areas_Arr_Of_Objs = [];
if (mergedAreas.isNull != true) {
var arrlen = mergedAreas.areas.items.length;
for (var ai = 0; ai < arrlen; ai += 1) {
var obj = mergedAreas.areas.items[ai];
var rng_obj = {};
rng_obj["worksheet"] = obj["address"].split("!")[0];
rng_obj["rowIndex"] = obj["rowIndex"];
rng_obj["columnIndex"] = obj["columnIndex"];
rng_obj["rowCount"] = obj["rowCount"];
rng_obj["columnCount"] = obj["columnCount"];
rng_obj["end_rowIndex"] = obj["rowIndex"] + obj["rowCount"] - 1;
rng_obj["value"] = obj["values"][0].toString();
Merged_Areas_Arr_Of_Objs.push(rng_obj);
}
}
return Merged_Areas_Arr_Of_Objs;
};