I need to redefine script on Google Sheets - javascript

I need help with this script. Basically is working, but running on the same ranges for the whole sheets ("A4:V50") and I need it to run different ranges in each sheet.
Thanks!
function clearIPs() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var i = 0; i < 46; i++) {
var sheet = allsheets[i]
sheet.getRange("A4:V50").clearContent();
}
}

Create some type of Settings Objects. For example:
var Settings = {
Sheet1 : "A4:V50",
Sheet2 : "A4:B20"
}
And then in your code you can use this information like so:
for (var i = 0; i < 46; i++) {
var sheet = allsheets[i];
// lets get the right settings for that sheet
var sheetNumber = i + 1;
var range = Settings["Sheet" + sheetNumber];
// if there is no value for that sheet, lets add a default
if( !range ){
range = "A1:B20";
}
// now we can use that reference
sheet.getRange( range ).clearContent();
}

Related

Select Random cell that is not Blank

I am having a problem that my brain cannot figure out.
I have a script that I copied and modified that will Randomly select a cell that is not blank from reassign!A1:A10
in reassign Tab I have a Query that will filter another sheet if a checkbox is ticked
so in reassign!A1:A10 it will depend if how many checkbox are ticked
the output is it only selects the first cell which is A1
Script:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("reassign");
var range = sheet.getRange("A1:A10");
var values = range.getValues();
var newValue = "";
for(var i = 0; i < values.length; i++) {
if(values[i][0] != "") {
newValue = values[i][0];
break;
}
}
sheet.getRange("B2").setValue(newValue);
}
I am running out of ideas. Sorry
Thanks in advance
I tried researching for solutions, but I really can't figure it out.
This Script selects randomly but sometimes it selects blank cells
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("reassign");
var range = sheet.getRange("A1:A10");
var values = range.getValues();
var newValue = values[Math.floor(Math.random()*values.length)][0];
sheet.getRange("B2").setValue(newValue);
}
The explanation that you have provide is not clear to me at all. But perhaps this small change might make a difference.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("reassign");
var range = sheet.getRange("A1:A10");
var values = range.getValues();
var newValue = "";
for(var i = 0; i < values.length; i++) {
if(values[i][0] !== "") {
newValue = values[i][0];
break;
}
}
sheet.getRange("B2").setValue(newValue);
}
Random Selection of non blank values:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("reassign");
var range = sheet.getRange("A1:A10");
var values = range.getValues().flat().filter(e => e);
sheet.getRange("B2").setValue(values[Math.floor(Math.random() * values.length)]);
}
I'm not at the PC right now, but you may try to add this line before var newValue.....:
values = values.filter(function (n) {return (n != "")})

Import multiple Spreadsheets into 1 Sheet using AppScript

I have a script that imports data from different google sheets into one sheet, it works perfectly except the fact that every time when I'm running the script the imported data starts further down in the sheet. What do I need to change in the script that every time i start the script it will push the data starting the first row of the sheet?
function getData() {
get_IDs=["1pkcy4jx14odSN-Ir2Nqai89_0dJc7RiLapWxRWIURj4","1J8_cZsjEa9c8vktPBvqGvcdwUBAVkWT3NSdTnXp6tMk"];
var ss=SpreadsheetApp.getActiveSpreadsheet();
var copySheet =ss.getSheetByName("Sheet3");
copySheet.getRange('A1:Z').clear();
for(z=0;z<get_IDs.length;z++)
{
var ss1=SpreadsheetApp.openById(get_IDs[z]);
var sheets =ss1.getSheetByName("Export Department");
var sheetsRange = sheets.getDataRange();
var sheetsValues = sheetsRange.getValues();
for(var y = 1;y<sheetsValues.length;y++)
{
copySheet.appendRow(sheetsValues[y]);
}
}
}```
Use setValues() instead of appendRow() and increment a counter by the number of rows on each import:
function getData() {
get_IDs = ["1pkcy4jx14odSN-Ir2Nqai89_0dJc7RiLapWxRWIURj4", "1J8_cZsjEa9c8vktPBvqGvcdwUBAVkWT3NSdTnXp6tMk"]
var ss = SpreadsheetApp.getActiveSpreadsheet()
var copySheet = ss.getSheetByName("Sheet3")
copySheet.getRange('A1:Z').clear()
let pasteRow = 1
for (z = 0; z < get_IDs.length; z++) {
var ss1 = SpreadsheetApp.openById(get_IDs[z])
var sheets = ss1.getSheetByName("Export Department")
var sheetsRange = sheets.getDataRange()
var sheetsValues = sheetsRange.getValues()
sheetsValues.splice(0, 1)
copySheet.getRange(pasteRow, 1, sheetsValues.length, sheetsValues[0].length).setValues(sheetsValues)
pasteRow += sheetsValues.length
}
}

Search and return data using getRange() and getValue

I am trying to create a sheet (using Google Sheets) for our volunteers to search for, update, and/or add mentoring information (javascript).
I started with the option to search (function onSearch) and it appears to work but the information does not appear in the sheet (attached FYI). I'd appreciate help in making this run.
date entry sheet
REVISED:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form1");
var str = formSS.getRange("D3").getValues()[3]; //Search for info entered in Form1$D3
var datasheet = ss.getSheetByName("TRACKING");
var values = datasheet.getRange(2,1,2); //Datasheet where info will be retrieved
if (values == str) {
var values1 = values.getValues(); //// get the tracking data if it matchs search request
var i = 1;
myFunction().onSearch = i < values.length; i++;
{
var output = datasheet.getRange(); ///retrieve information from the Tracking spreadsheet and
//populate the information in the appropiate cells.
formSS.get("E8").datasheet.getValue(1),
formSS.getRange("E10").getdatasheet.getValue(2),
formSS.getRange("E12").datasheet.getValue(3),
formSS.getRange("E14").datasheet.getValue(4),
formSS.getRange("J8").datasheet.getValue(5),
formSS.getRange("J10").datasheet.getValue(6),
formSS.getRange("J12").datasheet.getValue(7),
formSS.getRange("J14").datasheet.getValue(8);
return }}}
function onSearch() {
var SEARCH_COL_IDX=0;
var RETURN_COL_IDX=0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form1");
`` var datasheet = ss.getSheetByName("TRACKING");
var str = formSS.getRange("D3").getValues()[3]; //column Index
var values = ss.getSheetByName("Form1").getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
RETURN_COL_IDX = i+1;
var values = [[formSS.getRange("E8").datasheet.getValue(1),
formSS.getRange("E10").getdatasheet.getValue(2),
formSS.getRange("E12").datasheet.setValue(3),
formSS.getRange("E14").datasheet.getValue(4),
formSS.getRange("J8").datasheet.getValue(5),
formSS.getRange("J10").datasheet.getValue(6),
formSS.getRange("J12").datasheet.getValue(7),
formSS.getRange("J14").datasheet.getValue(8)]];
}
}
}
Thanks for responding. No one had the answer, and I even read that what I was asking is not available in Google Sheets. I decided to use the filter function for each cell instead.
B3 is the search field TRACKING!C:C is the sheet to retrieve the
information Tracking!E:E is the matched column to return information.
I am new here and at programming but I hope this helps someone.
=IFERROR(FILTER(TRACKING!C:C,TRACKING!E:E=B3),TRUE)

Copying all the sheets in every workbook within a google drive folder

I Want a google script that will loop through every workbook(spreadsheet) within my google drive folder, then copying every sheet within to one sheet of my own. I already have a script but its set to copy only the first sheet, could we make it loop to copy all sheets in the spreadsheet
I've tried to change the indexing but it'll only pull one sheet
function getDataToMaster() {
var folder = DriveApp.getFolderById("******************"); //Define id of folder
var contents = folder.getFiles();
var file;
var data;
var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; //first sheet of the file, change by getSheetByName("NAME") if you want
while(contents.hasNext()){
file = contents.next();
if (file.getMimeType() == "application/vnd.google-apps.spreadsheet") {
var sheet = SpreadsheetApp.openById(file.getId()).getSheets()[0];//first sheet of the file, change by getSheetByName("NAME") if you want
var startRow = 1;
var data = sheet.getDataRange().getValues();
var colToCheck = 7;
for(var j = 0; j < data.length;j++){
if(data[j][colToCheck-1] != "copied"){
sheetMaster.appendRow(data[j]);
sheet.getRange((j+1), colToCheck).setValue("copied");
SpreadsheetApp.flush();
}
}
}
}
}
Spreadsheet.getSheets() returns an array of Sheets you can iterate over.
Suggestion: when you know you will iterate through each element in an array (i.e. you don't want to exit early through break or return to terminate a loop), then try Array.prototype.forEach, as it reduces the amount of variables you need to setup. The following is how I would rewrite your getDataToMaster function using forEach twice:
function getDataToMaster() {
var folder = DriveApp.getFolderById("******************");
var contents = folder.getFiles();
var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
while (contents.hasNext()) {
var file = contents.next();
if (file.getMimeType() !== "application/vnd.google-apps.spreadsheet") {
continue;
}
SpreadsheetApp.openById(file.getId()).getSheets()
.forEach(function (sheet) {
var colToCheck = 7;
sheet.getDataRange().getValues()
.forEach(function (row, index) {
if (row[colToCheck - 1] != "copied") {
sheetMaster.appendRow(row);
sheet.getRange(index + 1, colToCheck).setValue("copied");
SpreadsheetApp.flush();
}
});
});
}
}
It should work if you add a for loop underneath of your if statement like so
if (file.getMimeType() == "application/vnd.google-apps.spreadsheet") {
var sheetCount = SpreadsheetApp.openById(file.getId()).getNumSheets();
for(var k = 0; k < sheetCount; k++){
var sheet = SpreadsheetApp.openById(file.getId()).getSheets()[k];//iterates through each sheet in the file
var startRow = 1;
var data = sheet.getDataRange().getValues();
var colToCheck = 7;
for(var j = 0; j < data.length;j++){
if(data[j][colToCheck-1] != "copied"){
sheetMaster.appendRow(data[j]);
sheet.getRange((j+1), colToCheck).setValue("copied");
SpreadsheetApp.flush();
}
}
}
}
How getSheets()[Integer] works is that it gets the sheet with the matching index number as supplied. The first sheet in the spreadsheet is index 0, the second is 1, third is 2, and so on. This loop just gets how many sheets there are and iterates through the indexes performing the actions you wrote for each individual sheet.

Changing existing script for Finding Duplicates in Google Sheets and entering "yes" in the last column instead of coloring it

I am new here and have no idea what I am doing. I found a script online and thought it would work for what I needed, but I would like to change it some. Any help would be appreciated. I have already changed it a little. If it would help to share the form with someone I can do that since it is only in the testing stages.
What I am wanting to do is instead of coloring the duplicates red in the main sheet I want it to put "Yes" in the last column of the main sheet. I would also like it to ignore the blank rows. Is this possible? (I copied the script I am currently using below)
function findDuplicates() {
// List the columns you want to check by number (A = 1)
var CHECK_COLUMNS = [13];
// Get the active sheet and info about it
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("USE THIS SHEET FOR DATA").activate();
var numRows = sourceSheet.getLastRow();
var numCols = sourceSheet.getLastColumn();
// Create the temporary working sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = ss.insertSheet("FindDupes");
// Copy the desired rows to the FindDupes sheet
for (var i = 0; i < CHECK_COLUMNS.length; i++) {
var sourceRange = sourceSheet.getRange(1,CHECK_COLUMNS[i],numRows);
var nextCol = newSheet.getLastColumn() + 1;
sourceRange.copyTo(newSheet.getRange(1,nextCol,numRows));
}
// Find duplicates in the FindDupes sheet and color them in the main sheet
var dupes = false;
var data = newSheet.getDataRange().getValues();
for (i = 1; i < data.length - 1; i++) {
for (j = i+1; j < data.length; j++) {
if (data[i].join() == data[j].join()) {
dupes = true;
sourceSheet.getRange(i+1,1,1,numCols).setFontColor("red");
sourceSheet.getRange(j+1,1,1,numCols).setFontColor("red");
}
}
}
// Remove the FindDupes temporary sheet
ss.deleteSheet(newSheet);
// Alert the user with the results
if (dupes) {
Browser.msgBox("Possible duplicate(s) found and colored red.");
} else {
Browser.msgBox("No duplicates found.");
}
};

Categories

Resources