繁体   English   中英

SetValues() 到 Google Sheet 中的两个不同的单元格范围

[英]SetValues() to two different cell ranges together in Google Sheet

我有两本电子表格书。

预约一个

书B

我将 BookB 的工作表名称存储在 BookA 中。 我想搜索 BookB 中与 BookA 中存储的工作表名称匹配的所有工作表。 如果找到工作表,则获取单元格“A3”中的值并将其粘贴到 BookA 中相应工作表名称的前面。 (我已经成功完成了这个任务。问题来了。振作起来)

我想从 BookB 的工作表中获取没有重复的“文件格式”详细信息,并将其粘贴到页面名称前面的 BookA 工作表中。 可能是我的方式不正确。 如果有人可以提供帮助,我将不胜感激。

请注意,文件格式详细信息在给定的两张表中的两个不同范围内提及。 ALBW - D6:D21 和 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
            
          }
        }

这些编辑应该可以满足您的需求。 该脚本将在 B 书中查找名称在 A 中列出的工作表。一旦找到工作表,它将检查该工作表的 pmsRange 中的值是否包含 pmsSearchValue。 如果是这样,那么它将存储由“/”分隔的所有文件格式。 如果没有,那么它将存储''。 最后,在遍历从 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 利用您已经在问题示例中使用的应用程序脚本电子表格 Class

我设法让@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 部分。 否则它会跳过一个单元格并在错误的范围内给出结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM