Replace user in a protected range by script (Google Sheet) - arrays

here my specific case:
I have some range protected in google sheets
I need to replace some specific Editor if is editor of those range (var Editor2Replace and Editor2Add are emails)
Logically I tried to, for each sheet:
Cycle (FOR) of all the protected range (counter p)
For each protected range catch current editors and have it in array
Of the Editors read the email ==> this is what generate the mistake
Cycle (FOR) all the editors looking if someone of those is == Editor2Replace (that
is an email)
Here the code, but something is logically wrong, I doubt in what is an array and what not..
var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var p = 0; p < Protections.length; p++) {
var Protection_Desc = Protections[p].getDescription();
var Protection_Editors = [];
var Protection_Editors = [Protections[p].getEditors()];
for (var r = 0; r < Protection_Editors.length; r++){
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
if (Protection_Idontknow == Editor2Replace){
Protections[p].addEditor = Editor2Add;
Protections[p].removeEditor = Editor2Replace;
var Protection_Range = Protections[p].getRange();
var Protection_Row = Protection_Range.getRow();
var Owner1 = sheet.getRange(Protection_Row,5).getValue();
var Owner2 = sheet.getRange(Protection_Row,6).getValue();
if (Owner1 == Editor2Replace){
sheet.getRange(Protection_Row,5).setValue(Editor2Add);
}
if (Owner2 == Editor2Replace){
sheet.getRange(Protection_Row,6).setValue(Editor2Add);
}
}
}
Many thanks for hepling

There were a lot of issues in your script and I will enumerate them one by one. Also, I was able to replace a user in the protected sheet by modifying your script.
Issues:
Duplicate declaration
var Protection_Editors = [];
var Protection_Editors = [Protections[p].getEditors()];
Storing the returned value (array) in another array (which should not be done in your issue, it doesn't help you with anything)
var Protection_Editors = [Protections[p].getEditors()];
...
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
Newly declared variable having an index (which I don't understand why)
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
Variable not declared Protection_Idontknow
if (Protection_Idontknow == Editor2Replace){
Incorrect usage of methods addEditor and removeEditor
Protections[p].addEditor = Editor2Add;
Protections[p].removeEditor = Editor2Replace;
Below code should fix those issues (added some comments):
Code:
var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var p = 0; p < Protections.length; p++) {
var Protection_Desc = Protections[p].getDescription();
// returned value of getEditors is already an array, return as is
var Protection_Editors = Protections[p].getEditors();
for (var r = 0; r < Protection_Editors.length; r++) {
var Protection_Email = Protection_Editors[r].getEmail();
// compare current email with the one you want to replace
if (Protection_Email == Editor2Replace) {
// add new and remove the one to replace
Protections[p].addEditor(Editor2Add);
Protections[p].removeEditor(Editor2Replace);
}
}
}
Note:
I have removed anything that were unrelated to the replacement of editors.
Reference:
Protection

Related

Search for a value in a sheet using loop in Google Apps Script

The below code searches for a value found in SheetOne, Cell B60, in SheetTwo, Column A.
If the value is found in column A of SheetTwo, a message box will display the value of the cell adjacent to where the cell was found. My problem is though the code works, I would also like a message box to appear if no match is found. Everything I have tried either traps me in the loop, or yields no result.
{
var MyWB = SpreadsheetApp.getActiveSpreadsheet();
var ValueSheet = MyWB.getSheetByName("SheetOne");
var SearchValue = ValueSheet.getRange("B60").getValue()
var SearchSheet = MyWB.getSheetByName("SheetTwo");
var SearchRange = SearchSheet.getRange("A1:A").getValues();
for (var i = 0, len = SearchRange.length; i < len; i++) {
if (SearchRange[i][0] == SearchValue) {
var MyFound = SearchSheet.getRange(i+1,2).getValue()
Browser.msgBox(MyFound)
}
}
}
To show a message no matter what, you'll want to update an existing variable if you find a match, and otherwise print a default message:
var message = "";
for(i = 0; i < array.length; ++i) {
if(array[i] == myValue) {
message += sheet.getRange(i, column).getValue();
break;
}
}
Browser.msgBox(message.length ? message : "Did not find the desired value");

Google App Script - How to set a spreadsheet file value from a string formatted through just one array

I am a beginner of programming.
The code snippet below in my GS project works very well.
var nextRow = Sheet_current.getLastRow() + 1; // Line 1
var str = "1,2,3,4,5,6";
var temp = new Array(); //Line 3
temp = str.split(",");
var target = new Array(); //Line 5
for (var i = 0; i < temp.length; i++) {
target.push([temp[i]]);
}
Sheet_current.getRange(Sheet_current.getLastRow() + 1, 1, target.length, target[0].length).setValues(target); //Line 9
Results in my spreadsheet file when running the above code:
result
I use a string as input, then convert it into a temporary array (Line 3).
I continue to declare a target array, to pass the values of the temporary array to the target array. (Lines 5 to 7)
Finally, I use the target array to dump the values into my spreadsheet (vertically, each word in the target array corresponds to a row in the spreadsheet file) (Line 9).
Can someone help me how to optimize the code that only through just one array.
Sincerely thank.
If you just want to provide result on differents row and starting from the first column, you can use the appendRow() method. Here is a example of use in your case:
function myFunction() {
var Sheet_current = SpreadsheetApp.getActiveSheet();
var nextRow = Sheet_current.getLastRow() + 1; // Line 1
var str = "1,2,3,4,5,6";
var target = new Array(); //Line 3
target = str.split(",");
for (var i = 0; i < target.length; i++) {
Sheet_current.appendRow([target[i]]);
}
}
Or you can just made the modification you want on your first array to keep your structure like :
function myFunction2(){
var Sheet_current = SpreadsheetApp.getActiveSheet();
var nextRow = Sheet_current.getLastRow() + 1;
var str = "1,2,3,4,5,6";
var temp = new Array();
temp = str.split(",");
for (var i = 0; i < temp.length; i++) {
temp[i] = [temp[i]]; // To convert your Array[] on Array[][]
}
Sheet_current.getRange(Sheet_current.getLastRow() + 1, 1, temp.length, temp[0].length).setValues(temp);
}
But I don't recommend this method, since it's okay for little program, but can be tricky for larger one.

can I check a regular expression on one array element?

I am trying to test if a specific element in an array is a range of characters, from a-z lowercase. what am I doing wrong? I am very new to coding (1 month in) and I am probably trying to do stuff thats too hard for me.
var array ["a","b","c"];
var myRegularExpression = /[a-z]/;
if (myRegularExpression.test(array[index])) {
//do stuff
}
To give you a working example as #Tushar mentioned:
var arr = ["a","b","c","123"];
var myRegularExpression = new RegExp("^[a-z]+$");
var matchCount = 0;
for (var index = 0; index < arr.length; index++) {
if (myRegularExpression.test(arr[index])) {
//do stuff
matchCount += 1;
}
}
document.getElementById("result").innerText = matchCount;
Number of elements matching the regex "^[a-z]+$":
<div id="result"></div>

How to compare 2 arrays?

I have two arrays, namely combo and truecombo. The user fills the combo with MovieClips by clicking on various buttons on the stage, truecombo is the correct combination.
At any given point (enterFrame) Flash is checking whether the two are the same, if yes, then do some stuff. For the time being this is my code (altered several times, like with Typecasting the indices, adding .parent at the end of combo[o] etc. 2 things will happen, either one or the other.
Either the statement will not be satisfied, at which point the adding and chopping of the combo array will continue, or the condition will be instantly met when combo.length = 6. Check my code.
UPDATE: I have a dropbox file with my current code. Click this for FLA link and here is the SWF link stripped down as always for ease and security.
/*stage.*/addEventListener(Event.ENTER_FRAME, checkthis);
function checkthis(e:Event)
{
for(var o:int=0;o<= combo.length; o++)
{
if((combo[o] == truecombo[o]) && (combo.length==truecombo.length))
{
equal=true;
}
}
if (equal==true)
{
stage.removeEventListener(Event.ENTER_FRAME, checkthis);
endSeq();
}
}
function endSeq():void
{
bravo.play();
for (var i:int = 0; i < combo.length; i++)
{
var element:DisplayObject = combo[i];
element.parent.removeChild(element);
}
firebb.gotoAndPlay(2);
windbb.gotoAndPlay(2);
spiritbb.gotoAndPlay(2);
earthbb.gotoAndPlay(2);
}
This is how I push my new elements to the combo array.
function add(element:DisplayObject)
{
twist.gotoAndPlay(2);
element.width = WIDTH;
element.height = HEIGHT;
if (this.combo.length >= MAX_ELEMENTS)
{
removeChild(this.combo.shift());
}
this.combo.push(element as DisplayObject);
this.addChild(element);
this.reorder();
}
function reorder()
{
for (var i:int = 0; i < combo.length; i++)
{
var element:DisplayObject = combo[i];
element.x = OFFSET_X + (i * SEP_X);
element.y = OFFSET_Y;
}
}
And this is how I have my truecombo and its contents created.
var fireb:firebtn = new firebtn();
var spiritb:spiritbtn = new spiritbtn();
var earthb:earthbtn = new earthbtn();
var windb:windbtn = new windbtn();
var combo:Array=new Array();
const truecombo:Array = [fireb,windb,spiritb,windb,earthb,fireb];
Sorry for the lack of comments, I'd guess it's pretty self-explanatory. Thanks in advance.
I believe combo[o] & truecombo[o] are two instances of the same class & you want them to be matched. If that is the case you may consider :
getQualifiedClassName(combo[o]) == getQualifiedClassName(truecombo[o])
To match the way you did, you must ensure the objects lying inside truecombo be referring to the same ones on stage & not new instances.
EDIT:
It seems you do not break the loop when the match is a success. Use this instead :
function checkthis(e:Event)
{
for(var o:int=0;o<= combo.length; o++)
if((combo[o] == truecombo[o]) && (combo.length==truecombo.length)) {
equal=true;
break;
}
if (equal) {
stage.removeEventListener(Event.ENTER_FRAME, checkthis);
endSeq();
}
}
Here's a really simple loop:
var equal:Boolean=true
if(combo.length == truecombo.length) {
for(var i:int=0; i<combo.length; i++) {
if(combo[i] != truecombo[i]) {
equal=false;
break;
}
}
} else {
equal=false;
}
if(equal) {
//do whatever
}
This assumes both are equal, until we find out otherwise. So if the lengths are different, they are not equal. If the ith element is different, they are not equal.
In the end, you check if your flag equal is true and do whatever you want to do.

ActionScript - Array.sortOn() For Non-English Data?

this is an array of objects that i want to alphabetize:
var streets:Array = new Array();
streets.push({name:"Édouard-Montpetit"});
streets.push({name:"Alexandre de Sève"});
streets.push({name:"Van Horne"});
streets.push({name:"Atwater"});
now i'll sort my array:
streets.sortOn("name", Array.CASEINSENSITIVE);
//Sorted
Alexandre de Sève
Atwater
Van Horne
Édouard-Montpetit
the accent above the E in Édouard-Montpetit, and any other first letter with a non-english accent is sorted after Z.
any ideas how i can sort this correctly? i do not have access to the named data.
I know this is late, but for anyone going through this answer, you could pass a Collator
object to the Array.sort() method. A simple example from the documentation:
var words:Array = new Array("coté", "côte");
var sorter:Collator = new Collator("fr-FR", CollatorMode.SORTING);
words.sort(sorter.compare);
trace(words);// côte,coté
Hope this helps
I don't think you can do it with sortOn, as there's no way to tell flash to use a particular collaction for sorting text (at least, not that I'm aware of).
However, you could use sort and a custom sort function.
In this sort function, basically you want to strip all accents and do a case insensitive comparation. Replacing diacritics is easy and after that, you can safely use < and > for comparing the strings. A sort function is called by sort with two of the items to be sorted at a time. It should return a negative number if the first passed items sorts first , a possitive number if the second comes first and 0 if they sort equal.
function sortText(obj1:Object,obj2:Object):int {
var a:String = replaceDiacritics(obj1.name);
var b:String = replaceDiacritics(obj2.name);
if(a < b) {
return -1;
} else if(b < a) {
return 1;
} else {
return 0;
}
}
function replaceDiacritics(str:String):String {
str = str.toLowerCase();
str = str.replace(/á/g,"a");
str = str.replace(/é/g,"e");
str = str.replace(/í/g,"i");
str = str.replace(/ó/g,"o");
str = str.replace(/ú/g,"u");
str = str.replace(/à/g,"a");
str = str.replace(/è/g,"e");
str = str.replace(/ì/g,"i");
str = str.replace(/ò/g,"o");
str = str.replace(/ù/g,"u");
return str;
}
streets.sort(sortText);
A couple of notes about this. I know this method won't work for Spanish, as you have ñ, which is considered a letter on its own (not a regular n with a funny mark) and comes after n and before o. So, it's not possible to just replace accents and do a < / > compare. I think this is not a problem in French, but I could be wrong (not sure how Ç / ç is considered for sort purposes, for instance). Also, note that I haven't replaced all possible diacritics, so you'd want to add circumflexes (^) and umlauts (¨) to replaceDiacritics as necessary.
Edit
For a table based approach you could try something like the following. Each letter is assigned a number that reflects the sort order. As long as you can assume that any letter will have an absolute sort order (that is, context won't change how this works, which is not the case on some languages), it should give you good results.
Out of laziness, I built the table with a loop and just did what was neccesary to put "Ñ" between "n" and "o". I'm not considering any diacritics for sorting purposes, so they have the same value than their unaccented counterpart. But you could change this table as neccesary. Also, this table probably should be hardcoded for the required locale, but this code is just to give you an idea of how you could do this, not a full implementation (and it's probably not entirely correct from a purist perspective, but I think it could do the job). Also, in case we find a character that is not mapped, I'm falling back to its code point to determine how it sorts.
var sortTable:Object = buildSortTable();
function buildSortTable():Object {
var sortTable:Object = {};
var char:String;
var offset:int = 0;
for(var i:int = 1; i < 256; i++) {
char = String.fromCharCode(i);
if(char == "Ñ" || char == "ñ") {
offset--;
continue;
}
sortTable[char] = i + offset;
if(char == "N") {
sortTable["Ñ"] = sortTable["N"] + 1;
offset++;
}
if(char == "n") {
sortTable["ñ"] = sortTable["n"] + 1;
offset++;
}
}
sortTable["Á"] = sortTable["À"] = sortTable["Ä"] = sortTable["Â"] = sortTable["A"];
sortTable["É"] = sortTable["È"] = sortTable["Ë"] = sortTable["Ê"] = sortTable["E"];
sortTable["Í"] = sortTable["Ì"] = sortTable["Ï"] = sortTable["Î"] = sortTable["I"];
sortTable["Ó"] = sortTable["Ò"] = sortTable["Ö"] = sortTable["Ô"] = sortTable["O"];
sortTable["Ú"] = sortTable["Ì"] = sortTable["Ü"] = sortTable["Û"] = sortTable["U"];
sortTable["á"] = sortTable["à"] = sortTable["ä"] = sortTable["â"] = sortTable["a"];
sortTable["é"] = sortTable["è"] = sortTable["ë"] = sortTable["ê"] = sortTable["e"];
sortTable["í"] = sortTable["ì"] = sortTable["ï"] = sortTable["î"] = sortTable["i"];
sortTable["ó"] = sortTable["ò"] = sortTable["ö"] = sortTable["ô"] = sortTable["o"];
sortTable["ú"] = sortTable["ù"] = sortTable["ü"] = sortTable["û"] = sortTable["u"];
return sortTable;
}
function sortText(obj1:Object,obj2:Object):int {
var a:String = obj1.name.toLowerCase();
var b:String = obj2.name.toLowerCase();
var len_a:int = a.length;
var len_b:int = b.length;
var char_a:String;
var char_b:String;
var val_a:Number;
var val_b:Number;
for(var i = 0; i < len_a && i < len_b; i++) {
char_a = a.charAt(i);
char_b = b.charAt(i);
val_a = sortTable[char_a];
val_b = sortTable[char_b];
// this is just in case we have a letter that we haven't mapped...
// let's fall back to using its code point
if(isNaN(val_a)) {
val_a = char_a.charCodeAt(0);
}
if(isNaN(val_b)) {
val_b = char_b.charCodeAt(0);
}
if(val_a < val_b) {
return -1;
} else if(val_a > val_b) {
return 1;
}
}
// both strings are equal so far; so the sorter one (if any) must sort first
if(len_a < len_b) {
return -1;
} else if(len_a > len_b) {
return 1;
} else {
return 0;
}
}

Resources