Thursday, March 23, 2023

Copying Cells and Formatting Fonts with Excel scripts

Microsoft Excel Scripts uses typescript to allow you to automate repetitive tasks in Excel which saves your time and effort. In this article, we'll give you a few code snipts to do the basic tasks using excel scripts.

1. Copy data from one cell to another

This is one of the most used functionalities in excel. Use the code below to automate that using excel scripts.

function copyCell() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var source = sheet.getRange("A1");

  var destination = sheet.getRange("B1");

  source.copyTo(destination);

}

2. Change cell color

Excel Scripts can also automate the process of changing cell color, making it easy to change the color of multiple cells simultaneously.  See sample code which does that.

function changeColor() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var cell = sheet.getRange("A1");

  cell.setBackground("#f0f8ff");

}


3. Change font

We change fonts in our workbooks for many reasons. If you need to automate that using excel scripts here is the sample code. 

function changeFont() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var cell = sheet.getRange("A1");

  cell.setFontFamily("Arial");

}



4. Change font color

Again, changing font color is a must do when we develop a dashboard, reports, etc. in excel. See how we could do this.

function changeFontColor() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  var cell = sheet.getRange("A1");

  cell.setFontColor("#000000");

}

      

No comments:

Post a Comment

Featured Post

XLOOKUP in Excel: The Ultimate Guide to Dynamic Data Lookup

Excel is a powerful tool for analyzing data, but sometimes finding specific data points can be a headache-inducing task. That's where XL...