Google Sheets script to clear checkboxes across multiple tabs - javascript

I have a spreadsheet with 16 tabs. Each tab is the same format. All tabs have checkboxes in column A.
I am attempting to create a button that will clear ALL checkboxes on every tab.
I created a button and assigned this script...
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange('A3:A');
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == true) {
values[i][j] = false; // Modified
}
}
}
dataRange.setValues(values);
}
It clears the checkboxes only on the tab that the button exists on.
How do I modify this script to clear checkboxes across ALL tabs with one click?

Never done any google sheets scripting but I see this function getSheets() that might be useful? https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheets()
Could try iterating through all the sheets that way, something like:
function myFunction() {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i = 0; i < sheets.length; i++) {
var dataRange = sheets[i].getRange('A3:A');
var values = dataRange.getValues();
for (var j = 0; j < values.length; j++) {
for (var k = 0; k < values[j].length; k++) {
values[j][k] = false;
}
}
dataRange.setValues(values);
}
}

Related

Extracting URL Links out of Google Sheets cell containing other text as well

I am trying to write a function that takes a Google Sheet ID as an input and parses the spreadsheet, extracting URL links out of the cells when encountered. I wrote the following function based off of this answer and this blog post.
function getLinksFromSheet(sheetId){
var ss = SpreadsheetApp.openById(sheetId); //general case
var sheets = ss.getSheets();
sheets.forEach(sheet => {
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(1,1, lastRow, lastColumn);
var rangeValues = searchRange.getRichTextValues();
for (var i = 0; i < lastRow; i++){
for (var j = 0 ; j < lastColumn; j++){
Logger.log(rangeValues[i][j].getLinkUrl())
}
}
});
The function works well for extracting links from cells where the only text contents of the cell are the link (for example http://google.com). If, however, the cell contains other text that isn't a part of the link (for example Link: http://google.com), then the function returns null. Does anybody know of a way to modify my funtion so that it will still return the link from cells with other text besides a link in them?
for (var i = 0; i < lastRow; i++){
for (var j = 0 ; j < lastColumn; j++){
const runs = rangeValues[i][j].getRuns();
for (const v of runs) {
Logger.log(v.getLinkUrl())
}
}
}
Reference:
getRuns()

Get Coordinates of a Table Cell in JavaScript

I am working on a maze generating algorithm visualisation in JavaScript, I made a 5x5 grid but now I require that whenever i click a table cell(), I get its coordinates i.e in which row it is and in which column. How would I go about implementing this?
Here's what im doing right now:
cell.onclick = function() {
console.log(this)
}
Here's how Im generating the cells and rows:
for(let i = 0; i < rows; i++) {
let row = grid.insertRow();
for (let j = 0; j < columns; j++) {
let cell = row.insertCell()
}}
Edit: I played around with the code and implemented MauriceNino's solution.
Here's the code:
for(let i = 0; i < rows; i++) {
let row = grid.insertRow();
for (let j = 0; j < columns; j++) {
let cell = row.insertCell()
cell.row = i
cell.column = j
cell.onclick = function(evt) {
console.log(cell.row + 1)
console.log(cell.column + 1)
}
}
}

insert rows using sheets on google spreadsheets

I am in the process of adapting the code below to insert a full row after function get emails is run. the extra row function works so i thought i would add line to the end of the getEmails. But it adds around 30 extra rows, i only need one added.
sheet.insertRows(2)
below is the getEmail code.
function getEmails() {
var label = GmailApp.getUserLabelByName("pollux");
var threads = label.getThreads();
var row = 3;
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getPlainBody());
row++;
sheet.insertRows(2);
some helpful suggestions would be great.
Cheers Mark
Remove this line sheet.insertRows(2);
and place outside this for loop
for (var i = 0; i < threads.length; i++) {
Changed code to
function getEmails() {
var label = GmailApp.getUserLabelByName("pollux");
var threads = label.getThreads();
var row = 3;
sheet.insertRows(2);
for (var i = 0; i < threads.length; i++) {
var messages=threads[i].getMessages();
for (var m=0; m < messages.length; m++) {
sheet.getRange(row,1).setValue(messages[m].getPlainBody());
row++;
now adds line and moves row down as 1 as i need.

checking data attribute in a array

I have a list of events on a page. My end goal is to hide a purchase button (by adding a class to it) if the event has passed, using JQuery/Javascript. Each event has a 3 data attributes(month, day, year). I tried using the following method to cycle through an array:
var matches = document.querySelectorAll(".event-event");
var i = 0;
for (i = 0; i < matches.length; i++) {
var event = matches[i].getElementsByClassName('date');
var eventDate = event.getAttribute('data-date');
}
But it says that "getAttribute" is not a function, I've also tried ".attr" and it said the same thing.
var matches = document.querySelectorAll(".event-event");
for (var i = 0; i < matches.length; i++) {
var event = matches[i].getElementsByClassName('date');
if (event.length > 0) {
for (var j = 0; j < event.length; j++) {
var eventDate = event[i].getAttribute('data-date');
}
}
}
The getElementsByClassName method returns an array.
Try this:
var matches = document.querySelectorAll(".event-event");
for (var i = 0; i < matches.length; i++) {
var events = matches[i].getElementsByClassName('date');
for(var j = 0; j < events.length; j++) {
var eventDate = events[j].getAttribute('data-date');
}
}
events is an array that you must iterate through.

Get cell of 2D Matrix in Google Spreadsheet

I have this function as a custom function in Google Apps Script being used in a spreadsheet:
function MultiplyMatrix(m2, m1) {
var result = [];
for(var j = 0; j < m2.length; j++) {
result[j] = [];
for(var k = 0; k < m1[0].length; k++) {
var sum = 0;
for(var i = 0; i < m1.length; i++) {
sum += m1[i][k] * m2[j][i];
}
result[j].push(sum);
}
}
return result;
}
Is there a way to get a specific index of the array without getting the whole result matrix input into the spreadsheet, If so what is the syntax?
I have this:
I want just this:
As I cannot comment, I'll use an answer. Given how your question is formulated, a simple answer can be to change your custom function definition so that it takes two extra parameters, the row index and column index of the result you're interested in; hence your code becomes:
function MultiplyMatrix(m2, m1, row, col) {
var result = [];
for(var j = 0; j < m2.length; j++) {
result[j] = [];
for(var k = 0; k < m1[0].length; k++) {
var sum = 0;
for(var i = 0; i < m1.length; i++) {
sum += m1[i][k] * m2[j][i];
}
result[j].push(sum);
}
}
return result[row][col];
}
You can then call your function as =MultiplyMatrix(A1:B2, C3:D4, 1, 1) to get the value of the intersection of the second row and second column.
If that is not the answer you expect, I suggest you edit your question, tag it also as google-spreadsheet (javascript custom functions are specific to that), and clarify the requirements. A spreadsheet example could be useful for us all to help.

Categories

Resources