简体   繁体   中英

Creating a new Google Sheet based off a template and exporting data from Master Sheet to newly created sheets

I'm apart of a committee at my University that allocates $90,000 of funding by reading through different requests and allocating money accordingly.

We finalize the allocation by completing an NOL form that has information about how much money is allocated per different categories.

All of this information is in a mastersheet. I'm looking to code a script that takes data from 250 rows on the master sheet, and turn it into 250 unique Google Sheets that have the information imported from the master sheet, along with initializing each google sheet to follow a specific template.

I'm stuck on the part where I create a new sheet to copy my template onto the new sheet, updating the value on the new sheet, before storing the link of the new sheet on my master document.

Is this something that Google Apps script is capable of? I'll paste my (incomplete) code. Currently the links are being pasted on the master sheet, but the template is not currently imported and I get linked to a blank sheet (when it should include the template)

Also, is there an easier solution to my problem? Perhaps I don't need to create a new sheet but I can add on existing sheets (I would do this but this would be 250 additional sheets)

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Justin's Analysis"); 
  var range = sheet.getRange("A1:P23");
  var nolSheet = ss.getSheetByName(['NOL']);
  var nolTemplateRange = nolSheet.getRange("A1:J33")
  var data = range.getValues();
  //console.log(sheet.getMaxRows())
  // data[2][15] = "Test"
  // range.setValues(data)
  for (i = 1; i<= 22; i++){// loop through NOL creation process through all rows of data. 
    var clubName = data[i][1];
    var input = "NOL" + i + clubName;
    console.log(input)
    var ssNew = SpreadsheetApp.create(input);
    var sheet = SpreadsheetApp.getActiveSheet();
    // var nolSheet = ss.getSheetByName(['NOL']);
    // let nolTemplateRange = nolSheet.getRange("A1: J33")
    //var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
    nolTemplateRange.copyTo(sheet.getRange("A1:J33")); 

    
    nolSheet.copyTo(ssNew) // copies NOL into new spreadsheet
    
    //Copy Registration 
    let registration = data[i][9];
    let subscription = data[i][10];
    let incentive = data[i][11];
    let food = data[i][12];
    let other = data[i][13];
    let total = data[i][14];
    //Copy Subscription
    //Copy Incentives
    //Copy Food
    //Copy Supplies/Other

    // grab Copy of NOL into new created sheet. 
    // Input values from data into new sheet 
    // input new sheets URL into NOL row. 
    data[i][15] = ssNew.getUrl(); 

  }
  range.setValues(data)
  
}

The issue was that you're not able to copy data from one google sheet to a different google sheet using the copy to function. You would need to use the getValues function on the first sheet before setting the values on the second sheet. I found my answer from this StackOverflow Question: Copying google spreadsheet columns from one spreadsheet to another with a script

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