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

Turn off client-side hydration in Nuxt.js or Prevent leaking raw data in Nuxt.js

Working on a Web App built with Nuxt.js for Server-Side Rendering poses some challenges. To safeguard my backend data, I turned to asyncData and Axios for communication with the server. However, Nuxt.js inadvertently exposed my backend data to clients th ...

Is there a hierarchy to be followed within the <head> element?

I have been developing websites using HTML for nearly 3 years now, but since I had to teach myself, there are still a few things I am unsure about. One question that has recently become important is whether it is possible to create groups within the <h ...

How can the @blur event be added to the vue-bootstrap-typeahead in Nuxt/Vue application?

I am facing an issue where I want to trigger a function upon leaving an input field. The input in question is set up using vue-bootstrap-typeahead. Upon inspecting the DOM, I found that the structure of the input element looks like this: <div id="m ...

When attempting to display an identical image on two distinct canvas elements in Angular 6

I am facing an issue where the image is only rendered on the second canvas instead of both canvases. I need assistance in finding a solution to render the same image on both canvases. Below is a screenshot demonstrating that the image is currently only re ...

Guide to dynamically setting a value in an input field using JavaScript's document.querySelector

My goal is to input a value using Javascript. Check out my project link! Click on "add to cart" and then proceed to checkout to reach the checkout page. I am trying to automatically add a Zipcode value to the checkout page. I attempted this method but it ...

Converting object values to strings is a common practice during JSON posting operations

I encountered an issue with a date object when sending it to a NodeJS server. While the object is still preserved, the time gets converted to a string during the process. Is there a way to prevent this conversion? I tried parsing the object but received an ...

I'm having trouble getting NextJS dynamic routes to function properly on my local server

src/app/user/[username].js [username].js content: import { useRouter } from 'next/router' export default function User() { const router = useRouter() return ( <p> {router.query.username} </p> ); } Upon visiting ...

What is the best way to display a button only when a different element is in focus?

I am working on a feature where each row consists of two text inputs and a button. The goal is to show the button when a user focuses on one of the input fields, and hide it again when they lose focus. I have come up with this solution: const Input = ({inp ...

Retrieving data from radio buttons using React Hook Form

As I delve into learning React and Next.js, working with form submissions has been a breeze thanks to react-hook-form. However, I've hit a roadblock when it comes to handling radio buttons in my application. Specifically, there's a step where use ...

What methods can be utilized to conceal an if statement in code?

In my application, I am facing an issue with an external JavaScript file that contains multiple if statements. The problem arises when one of the if statements is still being executed even if the corresponding form is hidden. The flow of my application is ...

Turn off email alerts for items and folders in ALFRESCO 5.2

Here's a snippet of JS code I created to toggle notifications with a button click: (Action.min.js): var me = this, jsNode = record.jsNode, content = jsNode.isContainer ? "folder" : "document"; if (jsNode.hasAspect("cm:emailed") ...

Combining data from multiple API calls in a for loop with AngularJS

I'm working with an API that contains pages from 1 to 10 and my goal is to cycle through these page numbers to make the necessary API calls. app.factory('companies', ['$http', function($http) { var i; for (i = 1; i < 11 ...

Update the path dynamically in Next.js without the need to reload the page

Every time the user clicks on the continue button, a function is triggered. Within that function, I make the following call: push("/signup/page-2", undefined, { shallow: true }); All dynamic routes resembling /signup/[page].js that return <Component / ...

The Google Maps API allows all markers to be connected to a single infowindow

I've been grappling with this issue for some time now, but I just can't seem to find a solution! I'm retrieving data from a database in Laravel using Ajax and then attempting to display infowindows for each marker on Google Maps. The markers ...

Protractor unable to locate elements using by.repeater

What is the best method for targeting this repeater with Protractor? <a ng-repeat="item in filteredItems = (items | filter:'abc')">{{item}}</a> ...

What is the best way to add hidden columns in Telerik Grid MVC3?

I'm currently working with a grid where I need to hide certain columns using the following code: foreach (var attr in grid.Attr) .Columns(columns => { columns.Bound(attr.key) .Width(attr.width) .Visible(attr.isVisi ...

Unable to cycle through an array of objects in JavaScript. Only receiving output for the initial element

var people = new Array(); var individual = { first_name: "Padma", identification_number: 1, region: "India" }; people.push(individual); people.push([individual = { first_name: "Balaji", identification_number: 3, region: "India" }]); people. ...

Creating an object that tracks the frequency of each element from another object using JavaScript

I have a scenario where I need to create a new object based on the number of occurrences of specific minutes extracted from a timestamp stored in another object. Existing Object: { "data": { "dataArr": [ { ...

Implementing a codeigniter delete confirmation box with Javascript

I have tried numerous solutions on this site, but nothing seems to be working for me. I am trying to implement a pop-up window confirmation before deleting my data. Here is the code I am using: <a href="<?php echo site_url('admin/barang/delet ...

Convert a relative path to an absolute path using the file:// protocol

When I scrape a website with similar html content, I come across the following code: <a href="/pages/1></a> In addition to this, I have access to the window.location object which contains: origin:"http://www.example.org" This allows me to ...