Home > other >  Refining Script. Copy from Template to Exclude Specified Sheets
Refining Script. Copy from Template to Exclude Specified Sheets

Time:10-11

  function myFunction2() {
    // These variables are from your showing script.
    var templateSheetName = "Template";
    var sheetsToExclude = [templateSheetName, 'Sheet1', 'Sheet2'];
    var ranges = ['C12:C35', 'C5:C6', 'G16:G19', 'G4'];

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var ssId = ss.getId();
    var sheets = ss.getSheets().filter(s => !sheetsToExclude.includes(s.getSheetName()));
    var formulas = ranges.map(r => ss.getSheetByName(templateSheetName).getRange(r).getFormulas());
    sheets.forEach(s => ranges.forEach((r, i) => s.getRange(r).setFormulas(formulas[i])));
  }

part of the code that need to be modified:

var sheetsToExclude = [templateSheetName, 'Sheet1', 'Sheet2']; var ranges = ['C12:C35', 'C5:C6', 'G16:G19', 'G4']; can these be edited to get the info from a range from a specific sheet? 

if possible

can sheetsToExclude be values from Helper!A1:A8 can ranges be values from Helper1!B1:B4

Helper Sheet
    A               B              C
    1 Sheet1       C12:C35
    2 Sheet2       C5:C6
    3 Sheet3       G16:G19
    4 Sheet4       G4
    5 Sheet5       
    6 Sheet6       


    Goals: 
    1. Copy Template Formulas to Sheets Specified in Helper Sheet 
    2. Helper will contain the sheetnames in Column A. Just want to restrict it to be Helper!A1:A8 
    3. Helper will define the Range to be copied and where the data will be pasted (Same range) in Column B. Just want to restrict it to be Helper!B1:B4

CodePudding user response:

Try this modification:

 function myFunction2() {
    // These variables are from your showing script.
    var templateSheetName = "Template";
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var helperSheet = ss.getSheetByName("Helper");
    var sheetsToExclude = helperSheet.getRange('A1:A8').getValues().flat();
    var ranges = helperSheet.getRange('B1:B4').getValues().flat().filter(rng => rng != '');
    
    var sheets = ss.getSheets().filter(s => !sheetsToExclude.includes(s.getSheetName()));
    var formulas = ranges.map(r => ss.getSheetByName(templateSheetName).getRange(r).getFormulas());
    sheets.forEach(s => ranges.forEach((r, i) => s.getRange(r).setFormulas(formulas[i])));
  }

Changelogs:

  1. Added the "Helper" sheet so that you can set the sheets to exclude on Helper!A1:A8 and the range to get the formulas on Helper!B1:B4.
  2. Changed var sheetsToExclude = [templateSheetName, 'Sheet1', 'Sheet2']; to var sheetsToExclude = helperSheet.getRange('A1:A8').getValues().flat();
  3. Changed var ranges = ['C12:C35', 'C5:C6', 'G16:G19', 'G4']; to var ranges = helperSheet.getRange('B1:B4').getValues().flat().filter(rng => rng != '');

Explanation:

How this works is that the modified sheetsToExclude gets the values from Helper Sheet using getRange('A1:A8').getValues().flat(). using the flat() method to turn it into a 1d Array.

Using the same logic for the modified ranges, but had to use .filter() method to filter out empty array elements.

Modification part 2:

 function myFunction2() {
    // These variables are from your showing script.
    var templateSheetName = "Template";
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var helperSheet = ss.getSheetByName("Helper");
    var sheetsToInclude = helperSheet.getRange('A1:A8').getValues().flat();
    var ranges = helperSheet.getRange('B1:B4').getValues().flat().filter(rng => rng != '');
    
    var sheets = ss.getSheets().filter(s => sheetsToInclude.includes(s.getSheetName()));
    var formulas = ranges.map(r => ss.getSheetByName(templateSheetName).getRange(r).getFormulas());
    sheets.forEach(s => ranges.forEach((r, i) => s.getRange(r).setFormulas(formulas[i])));
  }

Changelogs:

  1. Changed var sheetsToExclude = helperSheet.getRange('A1:A8').getValues().flat(); to var sheetsToInclude = helperSheet.getRange('A1:A8').getValues().flat(); because as per understanding, you would want to change this instead of excluding the sheets on the range, you would want to define where to copy the values instead.
  2. Changed var sheets = ss.getSheets().filter(s => !sheetsToExclude.includes(s.getSheetName())); to var sheets = ss.getSheets().filter(s => sheetsToInclude.includes(s.getSheetName()));

Explanation:

This version, instead of excluding the sheets on range Helper!A1:A8 and defined ranges on Helper!B1:B4, this includes them to where the template will copy the formulas on the provided sheets defined on those ranges.

References:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

  • Related