JavaScript code to copy a specified column through the last column, and then paste it down to the last row

I have limited experience with JavaScript and I've been putting together the code I need by searching online resources and watching videos. My goal is to set multiple columns in row 4, starting from column 18 to the last column, as the active cells for copying and then pasting formulas down to the last row based on data in column 4. The current code selects all data from column 18 through the last column as intended, but it also extends the selection down to the last row unintentionally. Does anyone know how to correct this issue?

function CopyFormulasDown() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var sRA4 = ss.getRange(4,1); //First copy is A4 ..this one works as intended
  var lr = ss.getLastRow();
  var lc = ss.getLastColumn();
  var sRR4lc = ss.getRange('R4:'+ lc +'4'); //Trying to select R4 through last col within row4; also tried (4,18+':'+lc), but throws an error: Exception: Cannot convert '18:37' to int.
  var fillDownRangeA4 = ss.getRange(4,1,lr);
  var fillDownRangeR4lc = ss.getRange(4,18,lr,lc);
  
   // sRA4.activate(); //range cell A4 is copied
    //sRA4.copyTo(fillDownRangeA4);  //copied cell pastes formula down to last row ..WORKS!
    sRR4lc.activate(); //ROW4 COL18 is selected through to last col as intended, but also selects down to last row.... problem is I didn't tell it to select past row 4... not sure how to correct this
   // sRR4lc.copyTo(fillDownRangeR4lc); //commented out for now until I can get just row 4 from col 18 through last col copy to work
};

Answer №1

Wow, the moment I hit submit on my question, a brilliant idea struck me - what if I moved the lc to the numofcolumns section instead? For those curious minds out there, here's the updated code that now works flawlessly:

function UpdateFormulas() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var rangeA4 = sheet.getRange(4, 1); //select cell A4
    var lastRow = sheet.getLastRow();
    var lastColumn = sheet.getLastColumn();
    var rangeR4lc = sheet.getRange(4, 18, 1, lastColumn); //select R4 through the last column in row 4
    var fillRangeA4 = sheet.getRange(4, 1, lastRow);
    var fillRangeR4lc = sheet.getRange(4, 18, lastRow, lastColumn);

      rangeA4.activate(); //activate cell A4
      rangeA4.copyTo(fillRangeA4); //copy and paste formula down to last row ..it works like magic!
      rangeR4lc.activate(); //select ROW4 COL18 until the last column
      rangeR4lc.copyTo(fillRangeR4lc);
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to render Angular charts

Recently, I've been attempting to integrate angular charts using the chart.js bundle sourced from CDN links. The version of Angular being used is 1.5.7 and for chart.js it's 2.1.6. I encountered the following error message: angular.js:13708 T ...

Utilize JavaScript to parse JSON response data, calculate the average of a specified field while keeping track of

The data returned from the API call is structured as follows: var array = {"scores":[ { userid: "1", mark: 4.3 }, { userid: "1", mark: 3.8 }, { userid: "2", mark: 4.6 }, { userid: "2&quo ...

I'm struggling to figure out how to specify the data type for storing an array of objects in a variable using React.useState

I am currently working on extracting values from an .xlsx file using the SheetJS library. Below, I will provide the code snippets, errors encountered, and the different approaches I have attempted. Data extracted from var dataToJson: (6) [{…}, {…}, { ...

What is the best way to include a character in a string if there are no instances of the same character already present?

Looking for help with a function that can determine if a string contains a period and add one if it doesn't. So far, I'm struggling to make it either continuously add periods or not add any at all. function checkPeriod() { if (inputString.indexO ...

How to show dates in AngularJS using local formatting

One situation I'm dealing with involves setting a date for an event. The challenge is that the date picker needs to display the date in a localized format based on the country or region. I've been exploring various solutions in AngularJS, but so ...

Rotating the icon in Bootstrap Accordion upon opening

I am trying to customize a Bootstrap 4 accordion by conditional rotating the icon to point up when it is open and back down when closed. I managed to achieve this using CSS, but now I need to implement it conditionally based on active states rather than ev ...

I'm having trouble with my Typescript file in Vscode - every time I try to edit the css code, all the text turns red. Can someone

Check out this visual representation: [1]: https://i.stack.imgur.com/9yXUJ.png Followed by the corresponding code snippet. export const GlobalStyle = createGlobalStyle` html { height: 100%; } body { background-image: url(${BGImage}); ba ...

The best method for quickly loading a webpage that is over 20MB in size

My website is a single-page calendar featuring a full year's worth of images. With 344 requests and a total load size of 20MB, the page consists of a simple PHP script without a database. The requests include a CSS file (15KB), 4 JS files (20KB), two ...

What could be the reason why I am unable to choose my radio button? What error am I

The output can be found here... I am struggling to use a radio button and cannot seem to select it. Can someone please explain what issue I am facing? Any help would be greatly appreciated. const [value, setValue] = React.useState({ yes:"", no:"" ...

Displaying a certain div when clicked within a loop using Vue.js and Laravel

I am currently facing an issue with displaying a hidden div upon click. The problem arises when using a loop to dynamically generate all the divs. Whenever I click the button, it shows all the divs instead of just one specific div on each click. I attempte ...

Discovering package utilities with npm commands

When integrating a package into my code, such as: import { Text, View, StyleSheet } from "react-native"; How can I discover the full range of utility functions like Text, View, etc. that are available in the react-native package? Is there an n ...

PHP data is not displayed by Ajax

I seem to be encountering a bit of trouble. I am attempting to utilize ajax to retrieve data from a PHP server, which in turn fetches it from a MySQL database, and then display it within a specific HTML tag location. However, for some unknown reason, nothi ...

Issue with Node.js MongoDb query results in the retrieval of all documents instead of the specific

Example of Json Structure: {"address": {"building": "1007", "coord": [-73.856077, 40.848447], "street": "Morris Park Ave", "zipcode": "10462"}, "borough": "Bron ...

Merging two distinct arrays of objects in JavaScript can be achieved by utilizing various methods and

I have a challenge where I need to merge two arrays of objects in a nested way. var array1=[{ PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2& ...

Necessary within a JavaScript Class

As a newcomer to using classes in JavaScript, I've been exploring the best practices and wondering about how 'requires' work when used within a class. For example, let's say I want to craft an IoT Connection class for connecting to the ...

Creating a canvas with multiple label introductions can be achieved by following these

I am looking to group labels and sublabels on the x-axis of a canvas component. Currently, I only have sublabels like: RTI_0, RTI_1... I would like to add labels to these sublabels like: RTI = {RTI_0,RTI_1,RTI_2] BB = {BB_0, BB_1,BB_2,BB_3] <div cla ...

Encountering a problem with AngularJS when attempting to access an array and display an alert message

While working with Angular, I encountered an issue in trying to access content stored within an array. Upon reviewing the code, console.log(JSON.stringify($scope.lineItems)) was returning [[]]. However, when inspecting or setting a breakpoint on this line ...

AngularJS directive doesn't refresh template when scope values are fetched dynamically through ajax requests

Attempting to give this question a precise title as possible, I find myself struggling with an issue in AngularJS. While trying to showcase my problem through a jsfiddle, it turned out to be too reliant on multiple files and not yet accessible online. So p ...

Issues persist with $.getJSON

I apologize if this question has been addressed previously, but I couldn't find the answer I was seeking. My issue involves using $.getJSON to send variables to a PHP file. Although the file returns success as true, it always triggers the .fail functi ...

Calculate the sum of multiple user-selected items in an array to display the total (using Angular)

Within my project, specifically in summary.component.ts, I have two arrays that are interdependent: state: State[] city: City[] selection: number[] = number The state.ts class looks like this: id: number name: string And the city.ts class is defined as f ...