Turn index match statement into Google Script - javascript

I am combining tables from different sheets into one on google sheets. What I am trying to do is set up a function onedit(e) may be combined with a loop function to run this
=index($G$2:$G$8,match($A10&$I$1,arrayformula($B$2:$B$8&$E2:$E8),0),"").
I am new to google script so I have just tried using formulas but the formula updates all cells next to it and what I want is that it matches and stays so that next time column G gets updated then the result be added to B10 instead of rewriting A10. (I1 has formula =today() so that it has a place of reference but if the code can just match with the date then that would be great!)
Here is the link to the sheet if you would like to see what I am talking about. https://docs.google.com/spreadsheets/d/1jviYYhI1V_bL8TMVvijR9vo_7sBFfktbhgblGNcYfrQ/edit?usp=sharing
Column B2:B8 are drop-downs that are based on the list from A10:A18 if that makes any difference.
Any help would be greatly appreciated I've been looking for 3 days and can't find something that would do this.

try this:
function autoload() {
var ss = SpreadsheetApp.getActiveSheet();
var meeting = ss.getRange("B2:F8").getValues();
var targetNames = ss.getRange("A10:A18").getValues();
var writtingRange = ss.getRange("B10:F18").getValues();
var updatedUser = targetNames;
var writtingColumn = -1;
var coolMatrix;
for (var i = 0; i < meeting.length; i++) {
var name = meeting[i][0]
for (var j = 0; j < targetNames.length; j++) {
if (targetNames[j][0] == name) {
for (var z = 0; z < writtingRange[0].length; z++) {
if (writtingRange[j][z] == '' && writtingColumn < 0) {
writtingColumn = z;
break;
}
}
writtingRange[j][writtingColumn] = meeting[i][4];
updatedUser[j] = true;
}
}
}
whiteSpaceRemover(writtingRange);
ss.getRange("B10:F18").setValues(writtingRange);
}
function whiteSpaceRemover(multidimensionalArray) {
for (var i = 0; i < multidimensionalArray.length; i++) {
for (var j = 0; j < multidimensionalArray[0].length; j++) {
if (multidimensionalArray[i][j] == '') {
multidimensionalArray[i].splice(j, 1);
multidimensionalArray[i].push('');
}
}
}
for (var i = 0; i < multidimensionalArray.length; i++) {
if (multidimensionalArray[i][0] == '') {
for (var j = 0; j < multidimensionalArray[i].length; j++) {
if (multidimensionalArray[i][j] != '') {
whiteSpaceRemover(multidimensionalArray);
}
}
}
}
return multidimensionalArray;
}
Tested this with a copy of your sheet, and it was working well, it sets the correct note next to the name and a '-' if they were not in the meeting, you have to run it manually for now, but it can be assigned to an onEdit() if you'd like.

function onEdit(event)
{ var timezone = "GMT-5"; var timestamp_format = "MM/dd/yyyy"; // Timestamp Format.
var updateColName = "Student Name";
var timeStampColName = "Date";
var sheet, ss;
ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getActiveSheet();//Get the active sheet
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sh
eet.getRange(4, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index >=5 && index <= 36 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format); cell.setValue(date); }
function onEdit(){
var ss = SpreadsheetApp.getActiveSheet();
var meeting = ss.getRange("B2:F37").getValues();
var targetNames = ss.getRange("A38:A90").getValues();
var writtingRange = ss.getRange("B38:F90").getValues();
var updatedUser = targetNames;
var writtingColumn = -1;
var coolMatrix;
for(var i = 0; i < meeting.length; i++){
var name = meeting[i][0]
for (var j = 0; j < targetNames.length; j++) {
if (targetNames[j][0] == name){
for (var z = 0; z < writtingRange[0].length; z++) {
if (writtingRange[j][z] == '' && writtingColumn < 0) {
writtingColumn = z;
break;
}
}
writtingRange[j][writtingColumn] = meeting[i][4];
updatedUser[j] = true;
}
}
}
for (var w = 0; enter code here w < writtingRange.length; w++){
if (writtingRange[w][writtingColumn] == ''){
writtingRange[w][writtingColumn] = '-';
}
}
ss.getRange("B38:F90").setValues(writtingRange);
}}

Related

Run This Script On Every Tab Except "Main" Tab

I have this script working well, but I'm uncertain how to make it run on every tab not labeled "Main". Thanks in advance!
function hideRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("Dude"); // Enter sheet name
var row = s.getRange('A2:A').getValues(); // Enter column letter that has the text "hide" and "unhide"
var cell = s.getRange('A1').getValue();
s.showRows(1, s.getMaxRows());
for(var i=0; i < row.length; i++){ if(row[i] != cell) { s.hideRows(i+2, 1); } // Value to hide
}
}
Jerome
Run on every sheet except Main
I assume that you are running the onEdit trigger.
function hideRows(e) {
const s = e.range.getSheet();
if(s.getName() == "Main") return;
var row = s.getRange(2,1,s.getLastRow() -1 ).getValues().flat();
var cell = s.getRange('A1').getValue();
s.showRows(1, s.getLastRow());
for (var i = 0; i < row.length; i++) {
if (row[i] != cell) { s.hideRows(i + 2, 1); }
}
}
Want to exclude more sheets
function hideRows(e) {
const s = e.range.getSheet();
const ex = ['Main','Sheetx'];//excluded sheet names
const idx = ex.indexOf(s.getName());
if(~idx) return;
var row = s.getRange(2,1,s.getLastRow() -1 ).getValues().flat();
var cell = s.getRange('A1').getValue();
s.showRows(1, s.getLastRow());
for (var i = 0; i < row.length; i++) {
if (row[i] != cell) { s.hideRows(i + 2, 1); }
}
}

Break if value from range on sheet1 is found in range on sheet2 not working

Basically, the user will input data on a new item on sheet NovoItem. When the Save Button (to be added) is pressed, the code is supposed to check on sheet2 (ArquivoItens) if the item is already there. If so, then it should break.
The code is not breaking if value is found on sheet2 (ArquivoItens):
function copyrange() {
var sourceSheet = 'Novo Item';
var destinationSheet = 'ArquivoItens';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var ActiveUser = Session.getActiveUser();
//it add current user and a timestamp to the last 2 columns on sheet1.
var val = sheet.getRange("Q12:Q")
.getValues();
for (var i = 0; i < val.length; i++) {
if (val[i] > 0) {
sheet.getRange(12, 34, i + 1, 1)
.setValue(new Date());
sheet.getRange(12, 35, i + 1, 1)
.setValue(ActiveUser)
}
}
var LastRowSource = sheet.getLastRow();
var LastColumnSource = sheet.getLastColumn();
var values = sheet.getRange(11,1,LastRowSource,LastColumnSource).getValues();
var csh = ss.getSheetByName(destinationSheet);
var data = [];
for (var i = 1; i < values.length; i++) {
if ( values[i][0] != '') {
data.push(values[i]);
//sheet.deleteRow(i+1)
}
}
var dataNovoItem = sheet.getRange("B12:B").getValues(); // gets data (item number) in the sheet where data is input
var dataArquivoItens = csh.getRange("B2:B").getValues(); //gets the item number on the datawarehouse sheet
for(var n=1; n < dataNovoItem.length ; n++){
for(var j=1; j< dataArquivoItens.length ; j++){
if (dataNovoItem[n] != 0 && dataArquivoItens[j] != 0) {
if(dataNovoItem[n] == dataArquivoItens[j]) {
break;
}
}
}
}
Logger.log("Novo Item" + dataNovoItem);
Logger.log("ArquivoItens" + dataArquivoItens);
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Here's the log I'm getting:
Any help is appreiated.
You aren't using the iterator to access the index that you're iterating through.
It should be
for(var n=1; n < dataNovoItem.length ; n++){
for(var j=1; j< dataArquivoItens.length ; j++){
if (dataNovoItem[n] != 0 && dataArquivoItens[j] != 0) { // this is where I try to eliminate blank rows in both ranges. I've tried != '', but it gets me the same result on the print below.
if(dataNovoItem[n] == dataArquivoItens[j]) {
break; //not working
}
}
}
}
Logger.log("Novo Item" + dataNovoItem);
Logger.log("ArquivoItens" + dataArquivoItens);
I ended up solving this by iterating only through the existing list and comparing each row with the value I had set on the current sheet. This is probably too logic to yur programmers, but for a beginner like me, this can be a pain.
This is how the working is now:
var newItemID = sheet.getRange("W5").getValue(); //get the destination cell, where the item will be set
var itemIDArquivoItens = csh.getRange(2, 1, csh.getLastRow(),1).getValues(); //gets the data range where the existing ID's to be compared against are
var message = "Item já cadastrado!"; //Message to be displayed in case the ID already exists in the list;
for(j = 1; j < itemIDArquivoItens.length; j++) { //loops through existing ID
if(itemIDArquivoItens[j][0] == newItemID) { //if the ID already exists, pop up a message and stop the code from running
Browser.msgBox(message)
return;
}
}

How to run script on all worksheets, Google Sheets

I have a script that deletes empty rows in Google Sheets. Currently it only runs on the sheet I have open; I want it to run on all worksheets in the document.
After a long time trying to apply code from other peoples questions, I just cannot seem to get it to work with my existing code.
Any answers are appreciated.
This is one snippet of code I tried to use but it would not run
function saveRange(){
var tabs = [
'Sample_tab_1',
'Sample_tab_2',
'Sample_tab_3',
'Sample_tab_4'
];
var ss=SpreadsheetApp.getActiveSpreadsheet();
for (var i = 0; i < tabs.length; i++) {
var sheet=ss.getSheetByName(tabs[i]);
var range=sheet.getRange('A8:AE47').getValues();
sheet.getRange(sheet.getLastRow()+1, 1, range.length, range[0].length).setValues(range);
}
}
This is what I have that works but only on the current sheet
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 0 || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Remove rows where column C is 0 or blank",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};
You can try this code snippet, but be careful, this will clear EVERY sheet in your current google Spreadsheet:
function readRows()
{
var spreadsheet = SpreadsheetApp.getActive();
var ttl = spreadsheet.getSheets().length;
for (var a=0; a<ttl; a++)
{
spreadsheet.getSheets()[a].clear()
//if you prefer to delete, please replace the above
}
}
Combined with your script, it may look like this below:
function readRows()
{
var spreadsheet = SpreadsheetApp.getActive();
var ttl = spreadsheet.getSheets().length;
for (var a=0; a<ttl; a++)
{
var sheet = spreadsheet.getSheets()[a];
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++)
{
var row = values[i];
if (row[2] == 0 || row[2] == '')
{
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
};
Here I would like to give a modification:
function readRowsModify()
{
var spreadsheet = SpreadsheetApp.getActive();
var ttl = spreadsheet.getSheets().length;
for (var a=0; a<ttl; a++)
{
var sheet = spreadsheet.getSheets()[a];
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
//if row 1 is header change i>0 to i>1
for (var i = numRows; i>0; i--)
{
var row = values[i-1];
//if in this row, column c is 0 or column c is '' or column c is undifined,
//please correct define in this filter to delete row
if (row[2] == 0 || row[2] == '' || row[2] == undefined)
{
sheet.deleteRow(i);
}
}
}
};

Unable to Delete Cells using JavaScript: The value provided (3) is outside the range

function selectTo(cell) {
var row = cell.parent();
var cellIndex = cell.index();
var rowIndex = row.index();
var rowStart, rowEnd, cellStart, cellEnd;
if (rowIndex < startRowIndex) {
rowStart = rowIndex;
rowEnd = startRowIndex;
sessionStorage.setItem('rowStart', rowStart);
sessionStorage.setItem('rowEnd', rowEnd);
} else {
rowStart = startRowIndex;
rowEnd = rowIndex;
sessionStorage.setItem('rowStart', rowStart);
sessionStorage.setItem('rowEnd', rowEnd);
}
if (cellIndex < startCellIndex) {
cellStart = cellIndex;
cellEnd = startCellIndex;
sessionStorage.setItem('cellStart', cellStart);
sessionStorage.setItem('cellEnd', cellEnd);
} else {
cellStart = startCellIndex;
cellEnd = cellIndex;
sessionStorage.setItem('cellStart', cellStart);
sessionStorage.setItem('cellEnd', cellEnd);
}
for (var i = rowStart; i <= rowEnd; i++) {
var TableID = sessionStorage.getItem("TableID");
var table6 = document.getElementById(TableID);
var row6 = table6.getElementsByTagName('tr')[i];
var rowCells = row6.getElementsByTagName('td');
for (var j = cellStart; j <= cellEnd; j++) {
rowCells[j].className = "hover";
}
}
}
var TableID = sessionStorage.getItem("TableID");
var cellStart = sessionStorage.getItem("cellStart");
var cellEnd = sessionStorage.getItem("cellEnd");
var rowStart = sessionStorage.getItem("rowStart");
var rowEnd = sessionStorage.getItem("rowEnd");
for (var i = rowStart; i <= rowEnd; i++) {
var myTable = document.getElementById(TableID);
var row10 = myTable.getElementsByTagName('tr')[i];
var rowCells = row10.getElementsByTagName('td');
for (var j = cellStart; j < cellEnd; j++) {
if (j === cellStart && i === rowStart)
continue;
//rowCells[j].style.display = "none";
myTable.rows[i].deleteCell(j);
}
}
When I delete cells, use a different method table.row[i].deleteCell(j); or removechild I get out of range error.
mesage:
Uncaught DOMException: Failed to execute 'deleteCell' on
'HTMLTableRowElement': The value provided (3) is outside the range [0,
3).
at init.callback
I find solution:
var listHover = document.querySelectorAll('.hover');
for (var i = 0; i < listHover.length; i++) {
if (i > 0) {
listHover[i].style.display = 'none';
}
}

replace string using array in javascript

abc1 abc2 abc3
abc4 abc5 abc6
abc7 abc8 abc9
using above csv file, load text by replacing the double quotes from the bellow sentence.
Sentence:
"" is going with "" to "" for something to know.
Expected out put is:
abc1 is going with abc2 to abc3 for something to know.
abc3 is going with abc5 to abc6 for something to know.
like this in javascript, php.
Code I have tried so far:
var s = 'Neque porro "" estqui "" dolorem';
var insert = [["a1", "b1"]["c1","d1"]];
console.log(insert);
var words = new Array();
words = s.split(" ");
console.log(words);
var count = 0;
for (i = 0; i < words.length; i++) {
for (j = 0; j < insert.length; j++) {
if(words[i] == '""')
s = s.replace(/""/, insert[j]);
}
}
console.log(s);
Hurray I got the answer:
reader.onload = function (e) {
var sent = ''; var sentt = ''; var senttt = '';
sent = $('#mixmsg').val();
var quoteLength = (sent.match(/""/g) || []).length;
var rowcells = [];
rows = e.target.result.split("\n"); alert(rows);
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].split(",");
rowcells.push(cells);
}
var rowcellso = rowcells.slice(0, -1);
console.log(rowcellso);
for (var ro = 0; ro < rowcellso.length; ro++) {
for (var scol = 0; scol < quoteLength; scol++) {
sent = sent.replace(/""/,rowcellso[ro][scol]);
console.log(rowcellso[ro][scol]);
} sentt +=sent+'\n'; sent= $('#mixmsg').val();
}
$('#container').html(sentt);
}

Categories