0
votes

I am using the splice Script to run code over numerous tabs

 function Slice_TABS() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheets = ss.getSheets().slice(7); // get 8th sheet onwards
 sheets.forEach(ss=>{
            
  Donor();
  })}

and am trying to use the below code to bring filtered results up to 10 per SKU. When i name the sheet the code works fine but i need it to run on the ActiveSpreadsheet. I am getting the following error " Type Error: sheet.getRange is not a function" (BOLD Below)

function GetNSKUSAG11() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheets(); 


 *//Building the matching set.
**var matchingValues = sheet.getRange('AF3:AF').getValues().filter(value => value[0] != 
'').map(nonBlankValue => [nonBlankValue[0], 0]);***


 //Filtering out the desired number of values
 var values = sheet.getRange("LOCATIONS!$A$3:$B").getValues().filter(value => 
 advancedFilter(value[0], matchingValues));

 let cols = values[0].length;
 let rows = values.length;
 //Printing out the found rows starting from AI
  sheet.getRange(2, 36, rows, cols).setValues(values);
  }


   function advancedFilter(value, toMatch) {
   let matched = toMatch.filter(couple => couple[0] === value);
   if (matched.length > 0) {
   if (matched[0][1]<10) { //Here you can decide when to stop returning SKUs
   matched[0][1] += 1;
   return true;
    }
    }
    return false;
    }

I think I am close.

1
At your script of GetNSKUSAG11, sheet of var sheet = ss.getSheets() is an array including the Sheet object. And sheet is directly used at var matchingValues = sheet.getRange('AF3:AF').getValues().filter... as the Sheet object. I think that this might be the reason of your issue. But I cannot understand about the top script in your question. So can I ask you about the detail of your goal? - Tanaike
Hi Tanaike - I have a changing number of tabs being created each time. So using Splice it will add the script donor(); to however many tabs are created after sheet 7. i just need the GetNSKUSAG11 to work on any activespreadsheet that it is required to do so. Currently you have to name the sheet but i cant becasue sheets are being created dynamically each time and names change regularly. - DUBYA RX
Thank you for replying. From your replying, how about modifying var sheet = ss.getSheets() to var sheet = ss.getActiveSheet()? - Tanaike
Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, can you post it as an answer? By this, it will be useful for other users who have the same issue. - Tanaike

1 Answers

1
votes

Answer to this question - thanks to Tanaike is

Change the code as per below

var sheet = ss.getSheets()

to

var sheet = ss.getActiveSheet()