Javascript 将SetValues()设置为Google工作表中的两个不同单元格范围
我有两本电子表格书 我在BookA中存储了BookB的表名。 我要搜索BookB中与BookA中存储的图纸名称匹配的所有图纸。如果找到图纸,则获取单元格“A3”中的值,并将其粘贴到BookA中相应图纸名称的前面。 (我成功完成了这项任务。问题来了。振作起来) 我想从BookB的工作表中获取“文件格式”的详细信息,不重复,并将其粘贴到BookA的工作表中页面名称的前面。可能是我的方式不对。如果有人能帮助我,我很感激 请注意,在给定的两张图纸中,文件格式详细信息在两个不同的范围内提及。ALBW-D6:D21和BFLCB-F6:F21Javascript 将SetValues()设置为Google工作表中的两个不同单元格范围,javascript,google-apps-script,google-sheets,google-sheets-formula,Javascript,Google Apps Script,Google Sheets,Google Sheets Formula,我有两本电子表格书 我在BookA中存储了BookB的表名。 我要搜索BookB中与BookA中存储的图纸名称匹配的所有图纸。如果找到图纸,则获取单元格“A3”中的值,并将其粘贴到BookA中相应图纸名称的前面。 (我成功完成了这项任务。问题来了。振作起来) 我想从BookB的工作表中获取“文件格式”的详细信息,不重复,并将其粘贴到BookA的工作表中页面名称的前面。可能是我的方式不对。如果有人能帮助我,我很感激 请注意,在给定的两张图纸中,文件格式详细信息在两个不同的范围内提及。ALBW-
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
}
}
这些编辑应该可以满足您的需要。脚本将在B册中找到名称列在A册中的工作表。一旦找到工作表,它将检查该工作表的pmsRange中的值是否包含pmsSearchValue。如果是,那么它将存储所有以“/”分隔的文件格式。如果没有,则它将存储“”。最后,在迭代从book A收集的每个图纸名称之后,它会将文件格式粘贴到您在示例中指定的粘贴范围中
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
}
}
参考文献:无。这主要是普通的javascript,它利用了您在问题示例中已经使用的应用程序脚本。我设法让@RayGun编辑的代码符合我的要求。非常感谢。如果其他人面临与我相同的问题,请在此发布代码
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
}
}
注意,您必须删除if语句的else部分。否则,它将跳过单元格并在错误的范围内给出结果。欢迎使用。我建议您添加一个示例,包括输入数据和预期输出。旁注:
pmsRange
没有定义我无法理解那么,如果单元格'A3'值等于Opera,我想得到单元格范围'F6:F21',得到该范围的唯一值,并将其与上述任务一起设置。
和如果单元格'A3'值等于'OnQ',则获取范围应为'D6:D21'。
。我为此道歉。我可以问一下你的剧本和目标的当前版本的细节吗?嗨@Tanaike,我已经编辑了这个问题。请看你是否能清楚地理解它。