How can one conditionally delete rows in bulk - if-statement

I am trying to delete rows in bulk which has values as web links starting with "www."
There are 1000+ such entries in my Google Spreadsheet which starts with "https://www." and "www."
I wish to delete only those starting with "www." and I tried this code in GAS.I'm pretty naive to GAS.
Unfortunately I'm missing out on some part of code due to which it is deleting all instances which has "www."
Below I have pasted the code snippet:
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var toDelete = [];
var re = new RegExp('www.');
for (var row = 0; row < values.length; row++) {
for(var column = 0;column<values[row].length;column++){
if (re.exec(values[row][column])){
toDelete.push(row);
}
}
}
for(var deleteRow = toDelete.length-1; deleteRow >= 0;deleteRow--){
sheet.deleteRow(toDelete[deleteRow]+1);
}
Also I did try out this which worked partially after tweaking.
Delete row in Google Sheets if certain "word" is found in cell

Related

Multiple Dropdownlist from another spreadsheet with more than 500 items in each

Hello and thanks in advance for your help,
I find many other topics about this, but I did not find answers to my problem, so here I am.
My goal is to automaticly create dropdow lists in a board (between 50-100 drop down list), according to a word in another column. The data should be in another spreadsheet, and most of the dropdown list could go around 600-700 items.
Please check in picture below for more explainations :
board picture
The dropdown list 1 and 2 are created in column G and H if it does find a sheet name in the first word of the column F "designation". If you pick an item of colum G "Libellés", it makes a Vlookup for column "MP" and price in the designated sheet. Same if you pick an item in of column "MP", it makes a Vlookup in "Libellés" and "price" to match.
This is working fine when all sheets are in the same spreadsheet, but I can't find a way to make it work when all data sheets are in another spreadsheet. The spreadsheet with the code will be copy many times (200+ each year), so I want to put the database sheets (15000+lignes x 5 columns) in only one spreadsheet that feeds all others "small" spreadsheets when we open those.
I tryed many options :
requireValueInList : This is not working, some lists are 600-700 items, not working with big lists like this.
list from a range : Ranges are too big, and I have around 50-100 dropdown list of 100-700 items, this is too much, program stops before the end (and I have a good computer, which is not the case of most of my colleagues), so this is not a solution, or I am doing it wrong.
requireValueInRange : This is what I am using to make it run when everything is on the same spreadsheet, but can't use it if datas are on another spreadsheet.
Is there a way to get around requireValueInRange limitation? This is the first time I use google app script so please don't judge too harshly.
here is the code, it is working fine when all data sheets are in the same sspreadsheet as the target sheet for drop dow list:
function onOpen(e) {
// sss for source spreadsheet where I have all datas, and tss for target spreadsheet where I want my lists to be created
var sss1 = spreadsheetApp.openById('blahblah');
var ssh1 = sss1.getSheetByName('Emballages');
var tss = SpreadsheetApp.getActiveSpreadsheet();
var tsh = tss.getActiveSheet();
var tsh1 = tss.getSheetByName('Fiche_eclate_CS');
// The below part is to update 2 lists, using datas from the sheet "emballages" in the source spreadsheet, all working fine except data validation rule, won't accept arguments
var sheet = tss.getSheets()[0];
var column = sheet.getRange("F:F").getValues();
var prow;
for (var i = 0; i < column.length; i++){
if (column[i][0] === "Packaging :") {
prow = i+1;
break;
}
}
var emblrow = ssh1.getRange('B:B').getLastRow();
var cel1D = tsh1.getRange(prow,7);
// This is where I have problems. The call ('B2:B'+ emblrow) is not accepted. I can't use this as data validation on another spreadsheet.
var cel2D = ssh1.getRange('B2:B'+ emblrow);
var ruleD = SpreadsheetApp.newDataValidation().requireValueInRange(cel2D).build();
cel1D.setDataValidation(ruleD);
var cel1M = tsh1.getRange(prow,8);
// Same problem here. The call ('A2:A'+ emblrow) is not accepted.
var cel2M = ssh1.getRange('A2:A'+ emblrow );
var ruleM = SpreadsheetApp.newDataValidation().requireValueInRange(cel2M).build();
cel1M.setDataValidation(ruleM);
// The below part is to update many lists, using datas from one sheet in the source spreadsheet. To find the good sheet in the other spreadsheet, I use the split function. This part is working fine. I only have problems with data validation rules, same as above.
var ui = SpreadsheetApp.getUi();
var button = ui.alert("Voulez vous mettre à jour toutes les listes de la feuille ?",ui.ButtonSet.YES_NO);
if (button == ui.Button.YES) {
var anchhrow;
var anchbrow;
var column1 = tsh1.getRange("B:B").getValues();
for (var i = 0; i < column1.length; i++){
if (column1[i][0] === "Anchor-haut") {
anchhrow = i+3;
break;
}
}
for (var i = 0; i < column1.length; i++){
if (column1[i][0] === "Anchor-bas") {
anchbrow = i;
break;
}
}
var rangeH1 = tsh.getRange('H'+anchhrow +':H'+anchbrow).getValues();
var range = tsh.getRange('H'+anchhrow +':H'+anchbrow);
var lrowH = rangeH1.length;
for (var k=0; k<lrowH; k++){
var rown = anchhrow + k;
var testrng = sheet.getRange("F" + rown);
if ( testrng == "" ) {
}
else {
var name = nom(tsh,rown);
var ssh3 = sss1.getSheetByName(name);
if (ssh3 == null){
}
else {
var flrow = ssh3.getRange('B2:B').getLastRow();
var rng1D = tsh1.getRange(k+anchhrow,7);
// same issu, can't use ('B2:B'+ flrow)for data validation
var rng2D = ssh3.getRange('B2:B'+ flrow);
var ruleD =
SpreadsheetApp.newDataValidation().requireValueInRange(rng2D).build();
rng1D.setDataValidation(ruleD);
var rng1M = tsh1.getRange(k+anchhrow,8);
// same here, can't use ('A2:A'+ flrow)for data validation
var rng2M = ssh3.getRange('A2:A'+ flrow );
var ruleM = SpreadsheetApp.newDataValidation().requireValueInRange(rng2M).build();
rng1M.setDataValidation(ruleM);
}
}
}
function nom(sheet,row){
var word = [{}];
var rng = sheet.getRange("F" + row);
var word = rng.getValue().split(" ");
Logger.log(word);
return word[0];
}
}
else {
}
// This part is where I control prices once all lists are updated, this is working fine.
var ui = SpreadsheetApp.getUi();
var button = ui.alert("Les listes ont été mises à jour. Voulez vous mettre à jour les prix ?",ui.ButtonSet.YES_NO);
if (button == ui.Button.YES) {
for(var j=0;j<lrowH; j++){
var cellI1 = tsh.getRange(j+anchhrow,8).getValue();
if (cellI1 == "") {
}
else {
var cellI2 = vlookup(ssh2,1,3,cellI1);
tsh.getRange(j+anchhrow,9).setValue(cellI2);
function vlookup(sheet, column, index, value) {
var lastRow=sheet.getLastRow();
var data=sheet.getRange(1,column,lastRow,column+index).getValues();
for(i=0;i<data.length;++i){
if (data[i][0]==value){
return data[i][index];
}
}
}
}
}
}
else {
}
Browser.msgBox ("MAJ feuille terminée");
}
Feel free to ask questions if there are stuffs you don't understand or if you need more details.
Thanks in advance,
The main issue you are encountering is due to the fact that you are calling the getActiveSpreadsheet method instead of the openById(id).
However, assuming that you have the same names for the sheets but they're just in different spreadsheets and you want to keep on using the requireValueInRange method, I suggest you add the following lines to your code:
Code
function createDataValidation() {
let spreadsheetIds = ['SS_1_ID', 'SS_2_ID', ...];
for (let i=0; i<spreadsheetIds.length; i++) {
var tss = SpreadsheetApp.openById(spreadsheetIds[i]);
var tsh = tss.getActiveSheet();
var tsh1 = tss.getSheetByName('Fiche_eclate_CS');
// the rest of your code
}
}
The code above is looping through all the spreadsheets - this is done by storing all the ids of the spreadsheets you want to use in the spreadsheetIds variable and afterwards, by using a for loop, is accessing each one of them and using the code you already have.
Reference
Apps Script SpreadsheetApp Class - openById.
I suggest
function betweenSpreadsheets() {
let localSs = SpreadsheetApp.getActiveSpreadsheet();
let distSsUrl = 'https://docs.google.com/spreadsheets/d/1H-Zh blah blah KHP7x8/edit?usp=sharing';
let distSsId = '1H-Zh blah blah KHP7x8';
let distSs1 = SpreadsheetApp.openById(distSsId)
let distSs2 = SpreadsheetApp.openByUrl(distSsUrl)
}
Then proceed as if the sheets were in the same spreadsheet.

Right alignment of text in table cell

I populate word table like this (code below). And I would like to insert text in variable run aligned to the right. I have tried many things but nothing works. Can you please help?
var entryTable = bm.getBookmarkStart().getAncestor(com.aspose.words.NodeType.TABLE);
if (entryTable != null) {
var myArray = ['A', 'B', 'C', 'D', 'E'];
var passes = myArray.length;
var index = 1;
while (...(id, myArray[0]+index)!="")
{
var cRow = entryTable.getLastRow().deepClone(true);
for (var j = 0; j < passes; j++) {
var run = cRow.getCells().get(j).getFirstParagraph().getRuns().get(0).deepClone(true);
var key = ...(id, myArray[j]+index);
run.setText(key);
cRow.getCells().get(j).removeAllChildren();
cRow.getCells().get(j).ensureMinimum();
cRow.getCells().get(j).getParagraphs().get(0).appendChild(run);
}
index++;
entryTable.appendChild(cRow);
}
entryTable.getRows().get(1).remove();
} else {
log.warn(LOG_PREFIX + "...");
}
You can use the following code to right-align all Paragraphs in a Table Cell.
Document doc = new Document("D:\\temp\\table.docx");
Table table = doc.getFirstSection().getBody().getTables().get(0);
for (Paragraph para : (Iterable<Paragraph>) table.getLastRow().getLastCell().getParagraphs())
{
para.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
}
doc.save("D:\\Temp\\18.11.docx");
Hope, this helps. I work with Aspose as Developer Evangelist.

Interactive Grid - model.getRecord(selectedRecords )returns null in oracle apex

I am trying to fetch all the selected records from the interactive grid in Oracle APEX by writing the below piece of code. I have also declared #myStaticIgId as the static Id of my report.
var record, USER_NAME;
var x = '';
var l_role = $v("P2_ROLE");
var l_justification = $v("P2_JUSTIFICATION");
var l_date = $v("P2_DATE");
//Identify the particular interactive grid
var ig$ = apex.region("myStaticIgId").widget();
//Fetch the model for the interactive grid
var grid = ig$.interactiveGrid("getViews","grid");
//Fetch the model for the interactive grid
var model = ig$.interactiveGrid("getViews","grid").model;
//Fetch selected records
var selectedRecords = apex.region("myStaticIgId").widget().interactiveGrid("getViews","grid").view$.grid("getSelectedRecords");
alert(selectedRecords.length);
//Loop through selected records
for (idx=0; idx < selectedRecords.length; idx++) {
//Get the record
record = model.getRecord(selectedRecords[idx][0]);
alert(record);
//Get the current value for USER_NAME
USER_NAME = model.getValue(record,"USER_NAME");
// USER_NAME = 'Abha';
alert('submit 2');
}
The alert statement that I have written for returning the record value, returns NULL to me.
Can you please suggest, what needs to be done so as to get the proper record value in record variable.
Regards,
Abha
Finally, I got it working. the issue was the ordering of the primary key column. The primary key column should be the first column in the list after "APEX$ROW_SELECTOR" and "APEX$ROW_ACTION".
My column "ASSIGNMENT_NUMBER" with the primary key was not the first column while column "USER_NAME" was the first column in the REgions. Hence it was working for USER_NAME and not for "ASSIGNMENT_NUMBER". After changing the order of this column as first column, It worked.
Hope this also helps somebody!
I tested your code (with some changes) here and works.
1 - Define the region ID of your Interactive Grid.
2 - Go to chrome -> open console (press F12) -> try this code:
var gridID = "orders";
var ig$ = apex.region(gridID).widget();
var grid = ig$.interactiveGrid("getViews","grid");
var model = ig$.interactiveGrid("getViews","grid").model;
var selectedRecords = grid.getSelectedRecords();
for (idx = 0; idx < selectedRecords.length; idx++) {
record = model.getRecord(selectedRecords[idx][0]);
console.log(record);
console.log(model.getValue(record,"USER_NAME"));
}
I tested this code in this page: https://apex.oracle.com/pls/apex/f?p=145797:8
I am also having similar issue. Flag is "Selected List"(Yes/No). I am trying to change Flag value from "No" to "Yes" for selected Rows, However value is not getting changed.
I am using below code.
function changeFlag(){
var isVerified;
var ig$ = apex.region("New").widget();
var grid = ig$.interactiveGrid("getViews","grid");
var model = ig$.interactiveGrid("getViews","grid").model;
var selectedRecords = apex.region("New").widget().interactiveGrid("getViews","grid").view$.grid("getSelectedRecords");
for (idx=0; idx < selectedRecords.length; idx++) {
record = model.getRecord(selectedRecords[idx][0]);
console.log(record);
isVerified = model.getValue(record,"FLAG");
console.log(isVerified);
if (isVerified === 'N') {
model.setValue(record,"FLAG", 'Y');
}
}
}
Sample application is available in this link
I am not understanding where i made issue.
Actual issue is Update the flag for bulk records using IG. I am trying to change the value to "Yes" and then will save the records. I am stuck because of this issue.

Applying color to rows in Sharepoint based on if an item is overdue

I'm using Sharepoint to develop an open issues list for my company that will automatically track if an issue is about to be due or overdue based on the color of the row. I have a way to override the default Sharepoint list features and edit the rows, but I think my date comparisons aren't functioning correctly. Here is my code so far:
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
OnPostRender: function(ctx) {
var statusColors = {
'Almost Due' : '#FFFF00',
'Overdue' : '#FF0000',
};
var rows = ctx.ListData.Row;
for (var i=0;i<rows.length;i++)
{
var due = rows[i]["Due Date"];
var duedate = new Date(due);
var rowId = GenerateIIDForListItem(ctx, rows[i]);
var row = document.getElementById(rowId);
var today = new Date();
if(duedate <= today) {
var status = 'Overdue';
}
Else if (due.toDateString - today.toDateString <= 7 && due.toDateString - today.toDateString >= 0){
var status = 'Almost Due';
}
row.style.backgroundColor = statusColors[status];
}
}
});
});
I need the row to change red if the issue is past due and the issue to change yellow if the issue is due within one week.
Modified example:
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
OnPostRender: function(ctx) {
var statusColors = {
'Almost Due' : '#FFFF00',
'Overdue' : '#FF0000',
};
var rows = ctx.ListData.Row;
for (var i=0;i<rows.length;i++)
{
var dueDate = new Date(rows[i]["DueDate"]);
var today = new Date();
var status = null;
if(dueDate <= today) {
status = 'Overdue';
}
else if (daysBetween(today,dueDate) <= 7 && daysBetween(today,dueDate) >= 0){
status = 'Almost Due';
}
if (status != null){
var rowId = GenerateIIDForListItem(ctx, rows[i]);
var row = document.getElementById(rowId);
row.style.backgroundColor = statusColors[status];
}
}
}
});
});
function daysBetween(startDate, endDate) {
var timeDiff = Math.abs(endDate.getTime() - startDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}
The list of changes:
Fixed some typos like Else keyword
Introduced daysBetween function for getting dates difference in
days
due.toDateString - today.toDateString ...
I suspect that due.toDateString is returning a function definition. If you want your code to actually run that function, you'd want due.toDateString().
That said, since you're trying to do math with these dates, it's best to keep them as datetime objects or numbers rather than convert them to strings. Otherwise your subtraction is going to surprise you with NaN and your comparisons are going to be perpetually false.
If you subtract a date object from another date object in JavaScript, you'll get the difference between the dates in milliseconds. Convert that to days and you'll be good to go!

in slickgrid how I can delete a row from a javascript function

how I can delete a row from a javascript function from a button
for example
If you're using a DataView, use the following:
DataView.deleteItem(RowID);//RowID is the actual ID of the row and not the row number
Grid.invalidate();
Grid.render();
If you only know the row number, you can get theRowID using:
var item = DataView.getItem(RowNum);//RowNum is the number of the row
var RowID = item.id
Suppose you are using jQuery
var grid;
$(function () {
// init options, load data
...
var columns = [];
columns[0] = {
id: 'id',
name: '#',
field: 'id', // suppose you have an id column in your data model
formatter: function (r, c, id, def, datactx) {
return 'X'; }
}
// init other columns
...
grid = new Slick.Grid($('#gridDiv'), data, columns, options);
}
function RemoveClick(databaseId, gridRow) {
// remove from serverside using databaseId
...
// if removed from serverside, remove from grid using
grid.removeRow(gridRow);
}
This is how i do it (not using any data provider though):
//assume that "grid" is your SlickGrid object and "row" is the row to be removed
var data = grid.getData();
data.splice(row, 1);
grid.setData(data);
grid.render();
I use this in a live project and it runs well. Of course, if you want to remove multiple rows then a few tweaks should be made, or if you use a data provider then you'd maybe want to remove the row only from the data provider and then have SlickGrid just refresh the rows.
Hope it helps :)
function deleteRows() {
var selectedIndexes = grid.getSelectedRows().sort().reverse();
var result = confirm("Are you sure you want to delete " + grid.getSelectedRows().length + " row(s)?");
if (result) {
$.each(selectedIndexes, function (index, value) {
var item = dataView.getItem(value); //RowNum is the number of the row
if (item)
dataView.deleteItem(item.id); //RowID is the actual ID of the row and not the row number
});
grid.invalidate();
grid.render();
}
}
var rowsToDelete = grid.getSelectedRows().sort().reverse();
for (var i = 0; i < rowsToDelete.length; i++) {
data.splice(rowsToDelete[i], 1);
}
grid.invalidate();
grid.setSelectedRows([]);
yes of course, I use it this way
var selrow = grid.getSelectedRows ();
data.splice(selrow, 1);
grid.invalidateAllRows();
grid.render ();
Greetings
hi
i'm used this script for delete row of SlickGrid
function deletefila(numrow)
{
alert("delete row"+numrow);
data.splice(numrow,1);
grid.removeAllRows();
grid.render();
//grid.removeRow(5);
//grid.updateRowCount();
//and then invalidate and re-render the grid by calling grid.removeAllRows() followed by grid.render().
}