cjwrk
cjwrk

Reputation: 295

Google AppScript GetRange Shorten Script

I was wondering if there was any way I could shorten the script below? As you can see, I copied the eight lines (B13-I13) to make a new one with a different row (B4-I14). However, I must continue it until row 23.

var values = [[formS.getRange("C7").getValue(),
              formS.getRange("C8").getValue(),
              formS.getRange("D9").getValue(),

              formS.getRange("B13").getValue(), <--- shorten this
              formS.getRange("C13").getValue(),
              formS.getRange("D13").getValue(), 
              formS.getRange("E13").getValue(), 
              formS.getRange("F13").getValue(),   
              formS.getRange("G13").getValue(),   
              formS.getRange("H13").getValue(),
              formS.getRange("I13").getValue(), ----> to this

              formS.getRange("B14").getValue(), <--- shorten this
              formS.getRange("C14").getValue(),
              formS.getRange("D14").getValue(), 
              formS.getRange("E14").getValue(), 
              formS.getRange("F14").getValue(),   
              formS.getRange("G14").getValue(),   
              formS.getRange("H14").getValue(),
              formS.getRange("I14").getValue(), ----> to this
]];

This is the script that I'm using..

function SaveData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var formS = ss.getSheetByName("Form");
  var dataS = ss.getSheetByName("Data");

  --> LOCATION OF THE SCRIPT ABOVE <---

  dataS.getRange(dataS.getLastRow()+1,1,1,11).setValues(values);
  ClearCell();
}

Please see the sample picture below where I encode the data. enter image description here

This is the sample image of the sheet where the data were being saved. enter image description here

SAMPLE SPREADSHEET

Upvotes: 1

Views: 83

Answers (1)

Tanaike
Tanaike

Reputation: 201428

In your situation, how about the following modification?

Modified script:

function sample() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var formS = ss.getSheetByName("Form");
  var dataS = ss.getSheetByName("Data");
  var values = [
    ...formS.getRange("C7:C8").getValues().flat(),
    formS.getRange("D9").getValue(),
    ...formS.getRange("B13:I23").getValues().flat()
  ];
  dataS.appendRow(values);
}
  • From However, I must continue it until row 23., I used B13:I23.
  • In this modification, the values of "C7:C8" and "D9" and "B13:I23" of "Form" sheet are copied to the last row of "Data" sheet as one row.

Upvotes: 2

Related Questions