简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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