google-sheetsgoogle-apps-script

How can I skip a specific column when copying from another sheet (setValues)?


Overview:

In Google Sheets, I have created a form, which inputs records and copies the supplied values to a subsequent sheet tab. This (Form) tab, contains a Submit Button, when selected the (9 values) are copied over to the next available/empty row in the subsequent tab.

In the subsequent tab (Call Tracker Records), I need to skip/bypass a specific column (C), when the values are being copying over. To clarify, there is an Array formula that is currently in column C and when the initial formula is run, it overwrites the formula.

The following columns should only receive new values: B,D,E,F,G,H,I,J,K. Previously I used the code snippet below, but it is including column C.

datasheet_Call_Tracker.getRange(getLastRowByDirectionUp(datasheet_Call_Tracker, "B")+1,2,1,9).setValues(values);

How can I skip column C from the list? Below is the full function for context. Thanks for further guidance.

function submitData(){

 //declare a variable and set the reference of active google sheet

 var myGoogleSheet = SpreadsheetApp.getActiveSpreadsheet();
 var shUserForm = myGoogleSheet.getSheetByName("Form");
 var datasheet_KPI = myGoogleSheet.getSheetByName("Monthly KPI Records");
 var datasheet_Call_Tracker = myGoogleSheet.getSheetByName("Call Tracker Records");
 var formType = SpreadsheetApp.getActiveSheet().getRange('B4').getValue();

     if (formType === 'Monthly Agent KPI') {



var user = Session.getActiveUser().getUserLoginId();

//to create the instance of the user-interface environment to use the alert features

var ui = SpreadsheetApp.getUi();

var response=ui.alert("Submit", "Do you want to Submit this Monthly Agent KPI record?", ui.ButtonSet.YES_NO);

//checking the user response

            if(response==ui.Button.NO)
            {
            return; //exit from this function
            }

    var values = [[
    shUserForm.getRange("E11").getDisplayValue() ,
    shUserForm.getRange("F11").getValue(),
    shUserForm.getRange("G11").getValue(),
    shUserForm.getRange("H11").getValue(),

    shUserForm.getRange("I11").getValue(),
    shUserForm.getRange("J11").getValue(),
    shUserForm.getRange("K11").getValue(),
    shUserForm.getRange("L11").getValue(),
    shUserForm.getRange("M11").getValue(),
    shUserForm.getRange("N11").getValue(),
    shUserForm.getRange("O11").getValue(),

    ]];

    datasheet_KPI.getRange(getLastRowByDirectionUp(datasheet_KPI, "A")+1,1,1,11).setValues(values);

// Code to clear Data entry Form fields

shUserForm.getRange('E11').setValue('');
shUserForm.getRange('F11').setValue('');
shUserForm.getRange('G11').setValue('');
shUserForm.getRange('H11').setValue('');
shUserForm.getRange('I11').setValue('');
shUserForm.getRange('J11').setValue('');

shUserForm.getRange('K11').setValue('');
shUserForm.getRange('M11').setValue('');
shUserForm.getRange('N11').setValue('');
shUserForm.getRange('O11').setValue('');

myGoogleSheet.toast("Monthly Agent KPI record added successfully!");

  //}

function getLastRowByDirectionUp(sheet, col) {
  if(sheet.getRange(col + sheet.getLastRow()).getValue()=="") {
    return sheet.getRange(col + sheet.getLastRow()).getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
  }
  else {
    return sheet.getLastRow();
  }

}
}else if (formType === 'Call Tracker') {



//to create the instance of the user-interface environment to use the alert features

var ui = SpreadsheetApp.getUi();

var response=ui.alert("Submit", "Do you want to Submit this Call Tracker record?", ui.ButtonSet.YES_NO);

//checking the user response

            if(response==ui.Button.NO)
            {
            return; //exit from this function
            }

    var values = [[
    shUserForm.getRange("E13").getDisplayValue() ,
    shUserForm.getRange("F13").getValue(),
    shUserForm.getRange("G13").getValue(),
    shUserForm.getRange("H13").getValue(),

    shUserForm.getRange("I13").getValue(),
    shUserForm.getRange("J13").getValue(),
    shUserForm.getRange("K13").getValue(),
    shUserForm.getRange("L13").getValue(),
    shUserForm.getRange("M13").getValue(),

    ]];

    datasheet_Call_Tracker.getRange(getLastRowByDirectionUp(datasheet_Call_Tracker, "B")+1,2,1,9).setValues(values);


// Code to clear Data entry Form fields

shUserForm.getRange('E13').setValue('');
shUserForm.getRange('F13').setValue('');
shUserForm.getRange('G13').setValue('');
shUserForm.getRange('H13').setValue('');
shUserForm.getRange('I13').setValue('');
shUserForm.getRange('J13').setValue('');
shUserForm.getRange('K13').setValue('');
shUserForm.getRange('L13').setValue('');
shUserForm.getRange('M13').setValue('');

myGoogleSheet.toast("Call Tracker record added successfully!");

  //}

function getLastRowByDirectionUp(sheet, col) {
  if(sheet.getRange(col + sheet.getLastRow()).getValue()=="") {
    return sheet.getRange(col + sheet.getLastRow()).getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
  }
  else {
    return sheet.getLastRow();
  }

}


}}

enter image description here


Solution

  • To avoid overwriting an array formula result area, replace the values in those columns with null values. To do that, use Array.splice() and Sheet.appendRow(), like this:

    function submitData() {
      const ss = SpreadsheetApp.getActive();
      const formSheet = ss.getActiveSheet();
      const ui = SpreadsheetApp.getUi();
      const formType = formSheet.getRange('B4').getDisplayValue();
      if (!formType.match(/^(Monthly Agent KPI|Call Tracker)$/i)) throw new Error(`Unknown form type ${formType}.`);
      const response = ui.alert('Submit', `Do you want to Submit this in ${formType} Records?`, ui.ButtonSet.YES_NO);
      if (response !== ui.Button.YES) return;
      const range = formSheet.getRange(formType === 'Monthly Agent KPI' ? 'E11:O11' : 'E13:M13');
      const targetSheet = ss.getSheetByName(formType === 'Monthly Agent KPI' ? 'Monthly KPI Records' : 'Call Tracker Records');
      const values = range.getValues().flat();
      values.splice(0, 0, null); // add null in the first column, pushing existing values to the right
      values.splice(2, 0, null); // add another null in the third column
      targetSheet.appendRow(values);
      range.clearContent();
      ss.toast(`${formType} updated successfully.`);
    }
    

    See Range.getValues(), Array.flat(), Array.splice() and Sheet.appendRow().