Home > other >  Copy data multiple times to complete table
Copy data multiple times to complete table

Time:05-18

I've developed a spreadsheet that records classroom specific data alongside student scores on the activity. The script I've written so far will copy the teacher information (name, period, assessment, and standard) as well as the standard score. That data is transferred to a data spreadsheet. I'm running up against the issue however that the teacher data is only associated with a single score entry. I need it to be associated with each score. (see poorly rendered image)

Poorly rendered image Desired Outcome

Subsequent saved entries fill below. This all works. I just can't get the range to fill down. Hopefully this makes sense. Code below.

function saveRecord() {
  
  
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const formWS = ss.getSheetByName("Form")
  const settingsWS = ss.getSheetByName("Settings")
  const dataWS = ss.getSheetByName("Data")
  const fieldrange = ["C4","D4","E4","F4","G4"]
  const fieldvalues = fieldrange.map(f => formWS.getRange(f).getValue())
  
  dataWS.appendRow(fieldvalues)
  
  const scoresdata = formWS.getRange(13, 4, formWS.getLastRow(), 1).getValues()
  dataWS.getRange(dataWS.getLastRow(),dataWS.getLastColumn(),scoresdata.length,1).setValues(scoresdata)
  
  
  console.log(fieldvalues)
  
  
 //need to fix this, watch the video, I deleated some 
}

function clearRecord(){
  fieldrange.forEach(f => formWS.getRange(f).clearContent())
 
}

CodePudding user response:

Try this:

function saveRecord() {
  const ss = SpreadsheetApp.getActive();
  const fsh = ss.getSheetByName("Form");
  const dsh = ss.getSheetByName("Data");
  dsh.appendRow(fsh.getRange("C4:G4").getDisplayValues()[0]);
  const scores = fsh.getRange(13, 4, fsh.getLastRow()).getDisplayValues()
  dsh.getRange(dsh.getLastRow()   1, 1, scores.length).setValues(scores)
}

CodePudding user response:

Description

I believe what you want is to fill in Columns A to D with the teacher info.

I'm not able to test this but I believe it will work.

I did not look at clearRecord because fieldrange is not defined.

Script

function saveRecord() {
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const formWS = ss.getSheetByName("Form");
  const dataWS = ss.getSheetByName("Data");
  const fieldvalues = formWS.getRange(4,3,1,5).getValues()[0];
  console.log(fieldvalues)
  
  const scoresdata = formWS.getRange(13, 4, formWS.getLastRow()-12, 1).getValues();
  scoresdata = scoresdata.map( row => fieldvalues.concat(row) );
  dataWS.getRange(dataWS.getLastRow() 1,1,scoresdata.length,scoresdata[0].length).setValues(scoresdata);
}
  • Related