.clearcontent not working on just one spreadsheet - google-apps-script

I have the strangest problem. Just one function seems not to be working on my Spreadsheet. I can setContent in a cell but I cannot clear it.
function buildCrawlGrid(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dynadotS = ss.getSheetByName('Dynadot');
dynadotS.getRange('b5').clearContent;
}
I am very familiar with the use of this function and never had an issue in the past. I have tried in a different sheet in the same spreadsheet and have the same issue. It's not that the spreadsheet object is misnamed because I was able to set content in the cell. I tried SpreadsheetApp.flush() to ensure it wasn't latency. Any ideas what might cause this apart from a corrupt spreadsheet?

I am a bit late...but maybe could be useful for someone reading this.
Try to change with () at the end
dynadotS.getRange('b5').clearContent();

Related

Google Sheet Error: Service Spreadsheets timed out while accessing document with id

I get this error "Service Spreadsheets timed out while accessing document with id ..." every time I run a very simple code, in which I am basically copying data from one google sheet to another using getValues() and setValues().
I don't think it is because of 5M cells limit, because the same exact function is working perfectly fine in another Google Sheet with even bigger size. So I really don't understand where the problem is.
I have tried to create an empty GS and run the function, so I am only pulling data without any other calculation, but still, it gives me the same error.
Any idea what the reason could be?
Here the code as reference:
function MyFunction(){
var pm_ss_0 = SpreadsheetApp.openById('...');
var pm_tab_0 = pm_ss_0.getSheetByName('...');
var pm_data_0 = pm_tab_0.getDataRange().getValues();
var target_ss_0 = SpreadsheetApp.getActiveSpreadsheet();
var target_tab_0 = target_ss_0.getSheetByName('...');
target_tab_0.clearContents();
var target_data_0 = target_tab_0.getRange(1, 1, pm_data_0.length,
pm_data_0[0].length).setValues(pm_data_0);
}
I solved the issue inserting a flush before and after the line where the error appeared.
SpreadsheetApp.flush();
ss.insertSheet("Report "+fogl.getName(), ss.getNumSheets()); //line with the error in my code
SpreadsheetApp.flush();
This issue has also been reported on Google's Issue tracker
Go there and star the issue so you get the updates on it.
This problem is more random than 95% of the commentary on the Web about it attests to. I just had this happen to me for the first time, and it even affected a Macro that did absolutely nothing but hide the Active Tab. I couldn't do anything with Script Editor.
I tried simply duplicating the document. BION, that was the end of the problem for me. Or at least, so far.

How to open filter view without opening a new tab or browser window using Apps Script

I would like to open filter views via button. If I understood it correctly, this should be possible by now. But I'm not sure, because I don't understand the whole communication on the following topic:
https://issuetracker.google.com/issues/36753410)
I have written the following code. When I execute this code, I always get the error: "Method getFilter(object) not found". What
What am I doing wrong?
function OpenFilter(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Name of the sheet'); //The sheet in which I want to open the filter view
var rg = sheet.getRange("S1:S2"); //There is the range where my Hyperlink is placed
var link = rg.getCell(1,1).getValues(); // The cell of the Hyperlink to test
var filter = sheet.getFilter(link);
SpreadsheetApp.setFilter(filter);
}
Unfortunately the method setFilter(filter) has not been implemented so far.
Also, the method getFilter() does not accept parameters, see here.
I recommend you to have a look here to find examples what you can do with filters at current stage.
Keep in mind that you need to combine SpreadsheetApp with the Advanced Sheets Service to achieve your goal.

Getting "We're sorry, a server error occurred. Please wait a bit and try again." But only on one sheet

So I have (what I assume is) a fairly long script to automate giving permissions in Google Sheets. To make it work, I have to first remove all the editors, otherwise Google just puts them in every category. I can't figure out how to stop that, short of removing them completely.
In my test file, I am the owner. Though I assume I need to add something that will add the script runner's account as an editor, just in case they are not. (edited to add: Apparently you can not remove yourself as editor, so even if you are not the owner, you can still run the rest of the script. Thank you, google! EDITED AGAIN: THAT IS A FILTHY LIE. I wish I had linked to the person who said this, because they are WRONG. You can remove yourself, I'm now waiting for the owner to add me back in. 9__6)
So I'm using this:
//removes all Editors
var SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var editors = SpreadSheet.getEditors();
for (var i = 0; i < editors.length; i++) {
SpreadSheet.removeEditor(editors[i]);
};
Which has been working, in this exact file, every time I test it. Yesterday, however, I got this error:
"We're sorry, a server error occurred. Please wait a bit and try
again."
referencing this line specifically:
var editors = SpreadSheet.getEditors();
But it works in the other test sheets I've made. I'm worried that it is just going to randomly not work when I deploy it on the floor, and I will have no idea why, or how to fix it.
Is there a better way to accomplish this? These sheets are live already, and have been shared with tonnes of people, so I need to either remove their edit permissions or switch their edit type to 'view'.
(I know you can do it for folders, but I would like it specifically for a file, since I am ultimately not the one who owns these files and who knows how they have been organised.)
Thank you for any help!
....
October 23rd, 2016 - It's happening again, on multiple different sheets. I commented everything out, and it is definitely this bit that is causing problems:
//removes all Editors
var SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var editors = SpreadSheet.getEditors();
for (var i = 0; i < editors.length; i++) {
SpreadSheet.removeEditor(editors[i]);
};
The getEditors is too much, man. Is there another way to do this? Is there another way to remove editors, or set them to 'view only' and then back to 'edit' when I'm done?
(If google would just put only the emails in each protection range as I've defined it instead of everyone, I would not have this problem. But they will not, and I have to remove everyone for this script to work.)
I have already looked at a lot of the linked answers, and they didn't seem to reply. Is getEditors too heavy to use reliably?
I am really not a scripter, and could use any links or guidance anyone could give.
I also can't seem to unaccept my own answer as an answer...
edited 11/14/2016 - Problem persists. The linked answer talks about using the "string replace function", and how changing that made the script work. I am not using that. The rest of my script is a repeat of this type of thing:
//Sheet1
var sh3 = ss.getSheetByName("Sheet1");
var protection = sh3.protect().setDescription('Sheet1 - Wizards'); //protects whole sheet
protection.addEditors(['wizard1#test.com',
'wizard2#test.com',
'wizard3#test.com']);
//adds email addresses to WIZARD range
var unprotected = sh3.getRange('A10:T');
protection.setUnprotectedRanges([sh3.getRange("B3:B3"),
sh3.getRange("C1"),
sh3.getRange("B72:B72"),
sh3.getRange("C2:K8"),
sh3.getRange("A10:T70"),
sh3.getRange("A75:Q")]);
//unprotects everything but frozen header. (unprotects the Sheet1 Legend too.)
var range = sh3.getRange('C2:K8'); //selects the range
var protection = range.protect().setDescription('Sheet1 - Legend'); //names the range
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1#test.com',
'wizard2#test.com',
'wizard3#test.com'
'supervisor1#test.com’]);
//put Sheet1 supervisor emails here
var range = sh3.getRange('C1'); //selects the range
var protection = range.protect().setDescription('Sheet1 - Part Number'); //names the range
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1#test.com',
'wizard2#test.com',
'wizard3#test.com'
'supervisor1#test.com’]);
//put Sheet1 supervisor emails here
var range = sh3.getRange('A10:T38'); //selects the range
var protection = range.protect().setDescription('Sheet1 - worker 1'); //names the range
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1#test.com',
'wizard2#test.com',
'wizard3#test.com',
'lead1#test.com’,
'worker1#test.com',
'worker2#test.com']);
//put emails here - Sheet1 worker level
var range = sh3.getRange('A43:Q'); //selects the range
var protection = range.protect().setDescription('Sheet1 - Artists 2'); //names the range`
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1#test.com',
'wizard2#test.com',
'wizard3#test.com'
'lead1#test.com’
'worker1#test.com',
'worker2#test.com']);
//put emails here - Sheet1 worker level
... Omg the spacing is so off, please ignore that and any commas I might have missed going through. I needed to quickly anonymise this so I can post it. The actual code is spaced right, and does have all the proper commas.
So what am I doing wrong??? Why is google freaking out over the getEditors() bit, and why only in certain sheets??
I saw somewhere else that this is just a catch-all error for "google has no idea wth just happened, but it didn't work. Try again later, and maybe it will work? Fingers crossed????"
Which is frustrating AS HECK because this is not a fancy function at all.
November 29th, 2016: Had a few spare minutes on my hands today, so I thought I'd try this script again. Still didn't work.
But oh my god I think I've cracked it! It's to do with the sharing settings! When the file was originally created, the 'anyone with a link' was selected as the option for sharing.
Well, I thought that didn't give enough security, so I switched my template to 'only specific people'. I then manually removed everyone, thinking maybe this would work.
Ran it again, it worked.
Tried it again, without manually removing, hoping that just changing the sharing settings would work. IT DID!!! So I went to fix the one far down the pipeline that was sitting there with no protections.
Turns OUT that you CAN remove yourself through scripting, even though I had read somewhere that google would not do that. So, since I am not the owner of the sheet, I am now locked out. Which will be another problem, and another question.
But as soon as I get this licked, or get the actual owner to run the script, I will update if this has actually solved it.
I will be so much happier when this works!
Posting an answer because my problem has disappeared? I didn't change anything, but I tried it this morning and it is working. Same sheet, same code, no server error.
This exact same code ran on different copies of this sheet (of which I was also the owner) at the same time as this stupid server error popped up on one, so I really don't know what happened.
I will just pray to the Google Gods and hope that nothing goes wrong. (Best plan, y/y?)
Thank you to Rubén and noogui for their input.
Had the same thing happen to me. Had this Gscript that i couldnt get to run for the life of me. Restarted computer. Signed out of everything. Reuploaded the script. Went to bed. Next morning everything worked fine.
I had a similar problem, but here is what I found ... basically I was linking to a library that was either removed or I lost permissions to. Since I wasn't using this particular library, I removed the library and now all my sheets work again.

range.requireValuesInList and requireValuesInRange seem to be broken

Up to this morning, the following code worked without any problems
function onOpen(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("B2:B100");
var dv = range.getDataValidation();
dv.requireValuesInList(["Shower","Shave","Shop"]);
dv.setShowDropDown(true);
range.setDataValidation(dv);
}
But this evening, both functions give the error message:
TypeError: Cannot find function requireValuesInList in object DataValidation.
TypeError: Cannot find function requireValuesInRange in object DataValidation.
Any idea what happened? are these functions being replaced?
Just to test, I created a new blank spreadsheet, pasted in the onOpen function and tried to run it... no work.
Any insight is greatly appreaciated.
If you print the return value of Object.keys(dv) [listing all the methods of the DataValidation object] you now get:
isAllowInvalidData,setAllowInvalidData,getCriteriaValues,setHelpText,toString,setCriteria,getCriteria
It seems the DataValidation prototype has just changed utterly, with no warning or documentation of the change. Google’s own documentation (http://developers.google.com/apps-script/reference/spreadsheet/data-validation) refers to the now-defunct DataValidation prototype.
Nice one Google!
Waqar, I’m not sure how you can claim getDataValidation() is undocumented. The DataValidation object returned by getDataValidation() was well-documented at the link I posted above.
getDataValidation() method in range object is undocumented so we can't say what happened to this actually. Best practice is to use only those methods and classes which are documented.
Here is a link for the same in issue tracker. You may check it.
https://code.google.com/p/google-apps-script-issues/issues/detail?id=2356

setNamedRange() outside of the spreadsheet container?

I've tried as many combinations as I could come up with.
My goal is to have a Google Apps Script running StandAlone or from a Library and be able to set Named Ranges in a spreadsheet.
As best I can figure it, the setNamedRange() method is only available from within the Spreadsheet container and only when you use SpreadsheetApp.getActiveSpreadsheet().
I tried using openById() to no avail. The method is just not available.
Thought I was clever and tried openById then setActiveSpreadsheet. I wasn't clever enough.
Update, I opened issue 1816 "Object become global, auto complete persists even when deleted" with google-apps-script-issues http://code.google.com/p/google-apps-script-issues/issues/detail?id=1816
Quite interesting behavior. Misled me into asking the wrong question
Looks to be a bug in the GAS editor.
The following function demonstrates how to set a named range in a standalone script.
function testNamedRange() {
var ss = SpreadsheetApp.openById('here is the spreadsheet id');
var range = ss.getRange('Sheet1!A1:B2');
ss.setNamedRange('TestRange', range);
var rangeCheck = ss.getRangeByName('TestRange');
var rangeCheckName = rangeCheck.getA1Notation();
}
The rangeCheckName variable contains the A1:B2 string.