[英]Javascript - Looping through spreadsheet merged cells
I am looping through a range of data in a Google spreadsheet in JavaScript. 我正在遍历JavaScript中的Google电子表格中的一系列数据。
The problem I'm having is I have some merged cells that have the same value, for example C:6 to E:6 all have the same value as those 3 cells are merged. 我遇到的问题是我有一些合并的单元格具有相同的值,例如C:6至E:6都具有与合并这3个单元格相同的值。
So in my loop it will show C:6 = 'Focus and targeting'
D:6 = ''
and E:6 = ''
What I'm trying to do is in my loop assign the 2nd and 3rd values to the first cell value. 所以在我的循环中它将显示C:6 = 'Focus and targeting'
D:6 = ''
和E:6 = ''
我想做的是在我的循环中将第2个和第3个值分配给第一个单元格值。
Here is what I have: 这是我所拥有的:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Audit');
var range = sheet.getDataRange();
var numRows = range.getNumRows();
var numCols = sheet.getMaxColumns();
var values = range.getValues();
var count = 0;
for (var i = 2; i <= numCols - 2; i++) {
var questionCategory = values[5][i];
if(questionCategory == undefined) {
continue;
}
if(questionCategory == "") {
// need to assign 2nd and 3rd cells of merged list to have the value of 1st cell in merged list
}
count++;
}
Here is what the data looks like: 数据如下所示:
Any ideas how I can achieve this? 有什么想法可以实现这一目标吗?
Here is custom function to get merged cell value: 这是获取合并单元格值的自定义函数:
function getMergedCellValue(cellRange)
{
if (!cellRange.isPartOfMerge())
{
return cellRange.getDisplayValue();
}
return cellRange.getMergedRanges()[0].getDisplayValue();
}
And updated code using this function: 并使用此功能更新代码:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Audit');
var range = sheet.getDataRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var values = range.getValues();
var count = 0;
var questionRowNum = 6;
for (var i = 0; i < numCols; i++)
{
// assign 2nd and 3rd cells of merged list to have the value of 1st cell in merged list
var questionCategory = values[questionRowNum-1][i] || getMergedCellValue(sheet.getRange(questionRowNum, i+1));
count++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.