Create Your First Code in Apps Script
Hello everyone,
In this article we will try to make a code in Apps Script to get data or value from a cell in Google Sheets and store it in another cell.
Apps Script is a coding platform in G Suite that can be used to build simple applications on top of existing apps such as Sheets, Docs, Forms, etc. With Apps Script, you only need a browser and a google account to start scripting. Apps Script allows you to extend and manipulate Google apps like Drive, Sheets, Docs, and Gmail, so you can run the process the way you want.
Create Apps Script File in Your Google Drive
Open your google drive account.
Right click > More > Google Apps Script.
If there is no Google Apps Script, select Connect more apps and search for Google Apps Script.
Open your Google Apps Script.
At the top of the page, you can rename your Apps Script File.
The name of your script file at the top of the page
You can rename your script file
Create a Google Sheets File in your G Drive and open it.
Set B1 Cell as a data or text that you want to copy to another cell.
Write this script down to your Apps Script :
function myFunction() {
var app = SpreadsheetApp.openByUrl ("your spreadsheet url");
var activeSheet = app.getSheetByName("the sheet name");
var text = activeSheet.getRange(1, 2).getValue();
activeSheet.getRange(4, 1, 4, 4).setValue(text);
}
Notes :
Function …… () {......} is a code that can be used to create a process the way you want
var app = SpreadsheetApp.openByUrl (......); is code to declare the active spreadsheet
var activeSheet = app.getSheetByName(.....); is code to declare the sheet in the active spreadsheet
var text = activeSheet.getRange(1, 2).getValue(); is code to get the value in the cell with row number 1 and column number 2 or cell B2
activeSheet.getRange(4, 1, 4, 4).setValue(text); is code to store the value that we have got before to the cell in row number 4 and column number 1 along 4 rows down and 4 columns to the right.
The code above also can be written by this :
function myFunction() {
var text = SpreadsheetApp.openByUrl("your spreadsheet url").getSheetByName("the sheet name").getRange(1, 2).getValue();
SpreadsheetApp.openByUrl("your spreadsheet url").getSheetByName("the sheet name").getRange(4, 1, 4, 4).setValue(text);
}
Run the script by clicking the play button.
Give permission to that script.
Congrats you have finished writing your first code in Apps Script to get data from a cell in Google Sheets and store it in another cell. Hopefully this article can be useful. Thank you
Comments
Post a Comment