I want to display a message on a google sheet. But I don't get it, and, after research here, in documentation, I don't get the answer.
I think that the problem is in "activate" the spreadsheet, where i need to display.
var SEGUIMIENTO = SpreadsheetApp.openById("MyTestediD");
var INF = SEGUIMIENTO.getSheetByName("NameOfSheet");
function TestMessage() {
INF.activate();
Browser.msgBox("Hello")
}
When i run.. nothing happen
I need the definition of Spreadsheet outside the function because I'm working in 2 Spreadsheet's by ID in more that one function.
i only need the correction in my code for display a simple message in the spreadsheet.
PD. i really cant find a simple example of that,
Update
This code it's part of a macro recorder of a Spreadsheet, the same "SpreadsheetApp.openById("MyTestediD");"
I don't know why you try to 'activate' a sheet. If you want display a message I assume you want to do it in the user's current sheet, so:
SpreadsheetApp.getUi().alert('Confirmation received.');
From https://developers.google.com/apps-script/reference/base/browser
The methods in this class are only available for use in the context of a Google Spreadsheet. Please use G Suite dialogs instead.
As you can see, Google is nicely asking you to use G Suite dialogs instead of Class Browser, so be nice too and follow their request.
When you say you want a message in a spreeadsheet, do you mean an alert message? If so, the answer is to use the code SpreadsheetApp.getUi().alert('Hello.'); when the TestMessage function is executed
var SEGUIMIENTO = SpreadsheetApp.openById("My TestediD");
var INF = SEGUIMIENTO.getSheetByName("NameOfSheet");
function TestMessage() {
INF.activate();
SpreadsheetApp.getUi().alert('Hello.');
}
Related
I am working on a project which needs to get communication from C++ code to JavaScript on a webserver. Currently, I have data sending properly and it's being received but the issue is that I cant use the data outside of the inner(onmessage) function. This still works fine to overwrite elements of the webpage, but the charts I'm trying to build cant get the live data. If I put the code for the chart inside the inner function the entire program freezes and I can't get the variable out of the function for me to use it in the parent either. Right now, I just want to get the data out of the inner function so I can use it. So, is there any way for me to pull that data out of the inner function and use it in the parent function?
Here is my code:
{
var x;
var ws = new WebSocket('ws://localhost:10011/');
ws.onmessage = function(event)
{
x = event.data;
var testing = document.getElementById('InnerFunctionOutput');
testing.innerHTML = "Run Time: " + x;
}
var testing = document.getElementById('ParentFunctionOutput');
testing.innerHTML = "Run Time: " + x;
}
When I run this code the output from the inner function is the constant stream of data and the output from the parent is a constant 1. If anyone could help me find a solution, it would be greatly appreciated.
One alternative solution is to put the functions for the charts inside the websocket function. That way, I wouldn't have to get data out of the function. however, this solution has its own set of problems. I will put a link below to a thread where I ask about this alternate solution if you are interested in that part.
Plotly and Websockets
Any help would be greatly appreciated.
Thanks,
Luke
I found a solution to the problem. To get the data out of the websocket function I overwrote a text element on the page and simply made that element invisible. After that, just read the text element in order to get it into the rest of the code. However, there could be a better way of doing this so please let me knw if there is.
Also, there is one issue I ran into with this solution. When I originally tried this with normal formatting document.getElementById('some id').innerHTML = some data; it didn't work. But when with adding "window" to the beginning window.document.getElementById('some id').innerHTML = some data; it just worked.
I am trying to get the content of a cell's formula from a custom function that I wrote. I'm just trying to get the formula stored in B2. In Google Sheets. I am getting "Exception: Authorization is required to perform that action. (line 5).". I tried revoking the authorization, adding the next first three lines and authorizing again. Nothing worked.
/**
* #OnlyCurrentDoc
*/
function test() {
return SpreadsheetApp.getActiveSheet().getRange('B2').getDataSourceFormula().getFormula();
}
I can confirm that the error is coming from getDataSourceFormula(). I tried replacing it with other functions, and they worked normally. What is the problem?
Thanks.
You are using incorrectly getDataSourceFormula() as you are combining it with getFormula().
getFormula() returns the top left cell formula of a range while getDataSourceFormula() returns the DataSourceFormula for the first cell in the range. Thus, getDataSourceFormula will only work for data sources, here is more official information regarding what these are.
As you didn't mention anything about data sources in your question I am assuming that you are not using them and therefore you code line should look like this:
return SpreadsheetApp.getActiveSheet().getRange('B2').getFormula();
I have two google spreadsheets, a master copy and a child copy. When I update the formulas in the master copy, I want it to update the formulas in the child copy too. I intend to create many copies of the child copy and don't want to update the formulas in each one every time something small has to change.
I've tried using IMPORTRANGE(), and it only gives me the values, not the formulas. So I thought to create my own script to get this done. I'm using the following code to attempt this:
function REMOTEDATA(inKey, inRange) {
var outData;
var ss = SpreadsheetApp.openById(inKey);
if (ss) {
outData = ss.getRange(inRange).getFormulas();
}
return outData;
}
//My manifest is the following:
{
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets"
],
"timeZone": "America/Denver",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER"
}
I'm very new to oAuth and I don't understand how to get this to work. When I attempt this it gives me the following:
"You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets (line 4)."
Can anyone tell me what I need to add to get this to work? If there's an easier way, I'm all ears.
EDIT:
After reading Ruben's comment below, I changed the way I was doing it to having a button on the sheet itself run the function. It fixed the first problem, but now I'm running into another problem. I can't copy all the data directly from one sheet to another. I'm trying to get formulas, values, and formatting. If I just use getFormulas() and setFormulas() with the ranges, it gets rid of all the values that aren't formulas, and it doesn't change the formatting. I've been trying to use copyTo() but it tells me I can't do this between spreadsheets. My current code is:
function REMOTEDATA() {
var MASTERKEY = "key";
var UPDATERANGES = ["A1:B200", "C7:C200", "D1:H200", "J1:L200", "N1:S200", "U1:X200", "z1:ac200", "AE1:AH200", "AJ1:AL200"];
var ss = SpreadsheetApp.openById(MASTERKEY);
var cur_ss = SpreadsheetApp.getActiveSpreadsheet();
UPDATERANGES.forEach(function(range) {
data = ss.getRange(range).copyTo(cur_ss.getRange(range));
});
}
My head is starting to hurt looking at the different methods for the Ranges Class. I can't see any methods to do this, but I feel like I'm missing something. Any other ideas?
EDIT 3:
Okay. I figured out a workaround. Basically what I did was copy the entire sheet from one spreadsheet to the other using the sheet.copyTo() function. Then after that I used the range.copyTo() to get what I wanted. Then I deleted the copied sheet using sheet.deleteSheet()
Custom functions run anonymously so they can't call services that require authorization, like openById.
Instead of using a custom function to execute your script use another method like a custom menu, assign the script to an image, etc. or just run it from the script editor.
NOTE: You will have to modify your script a bit or use a another function to pass the required arguments.
Hey good morning/afternoon/evening depending where you guys are at!
Just have a quick question for you guys. I've been stuck on this for a bit, and I've tried looking around, but i can't seem to find the answer for this specific issue.
So I'm in the process of trying to create a column chart using google scripts with the html service (since they UIApp service is depreciated now). I'm trying to retrieve the data from a spreadsheet have already created. But when i use this code, the page comes up blank as if there is some error in the background.
var sheet = SpreadsheetApp.openById("1gdRB6FFV426bAj95C0xJlqSucnnX0Z5ATVQdC2");
So here are some specifics:
1) I put this line in my Index.html file within a function I have called
drawChart();
2) I do have them wrapped in this tag.
<script type="text/javascript">
3) I know that this line of code is the cause of the issue because as soon as i comment it out my temp graph with temp data pops up, and page runs fine. But as soon as uncommented it blanks out the whole page.
Any ideas?
I'm wondering if I have to actually place this line of code within the "Code.gs" file, then some how transfer the data from Code.gs to my Index.html file. If that's the case can anyone point me in the direction to where I can follow the direction on how to do it?
Thank you guys in advance.
Sincerely,
Sicariuxs
You must use a withSuccessHandler(functionNameToReceiveData).
<script>
function getData() {
google.script.run
.withSuccessHandler(functionNameToReceiveData)
getData();
};
function functionNameToReceiveData(theDateReceivedHere) {
Logger.log('theDateReceivedHere: ' + theDateReceivedHere;
};
</script>
You can't use: var data = google.script.run.getData(); You must use a withSuccessHandler(functionNameToReceiveData).
#Sandy Good
Hey man I want to thank you for all your help. you're answer was actually great. But there was an additional mistake on top of the ones that you corrected me on. In my actual spreadsheet I had it as a "Date" value. But the format was not matching the data type in my "dataTable". It's working now though. I made all the changes you told me, but in addition I changed the value type from "Date" to "Text". Then in my for loop I wrote a "new Date()" function using the text as the format, and that worked here is some screenshots to show my end result.
link
link
I'm currently trying to scrape Google Keyword Tools with CasperJS and PhantomJS (both excellent tools, thanks n1k0 and Ariya), but I can't get it to work.
Here is my current process:
Log in with my Google Account (to avoid captchas in the Keyword Tools).
Navigate to the Keyword Tools page.
Fill in the search form and press Search.
I'm stuck at step 3: the search form is not a regular HTML form, I can't use Casper#fill(), so instead I'm accessing the fields directly. Here are some of the syntaxes I tried to change the value of the Word or phrase field:
this.evaluate(function() {
// Trying to change the value...
document.querySelector('textarea.sP3.sBFB').value = 'MY SUPER KEYWORDS';
document.querySelector('textarea.sP3.sBFB').setAttribute('value', 'MY SUPER KEYWORDS');
document.querySelector('textarea').value = 'MY SUPER KEYWORDS'; // there's only one <textarea> on the page
// Trying to change other attributes...
document.querySelector('textarea.sP3.sBFB').textContent = 'MY SUPER KEYWORDS';
document.querySelector('textarea').style.backgroundColor = 'yellow';
});
Nothing works. I'm doing a Casper#capture() right after to see what the field contains. As you can see, it confirms I am on the right page and that I am logged in, but the <textarea> is empty.
Strangely, I can access other parts of the DOM: I could change the text of a link that said Advanced Options and Filters to ___VINCE SAYS HELLO___ (see capture), by doing the following:
this.evaluate(function() {
document.querySelector('a.sLAB').textContent = '___VINCE SAYS HELLO___';
});
PS. I know scraping Google Keyword Tools is against the TOS, but I'm thinking this question might be of interest to anyone trying to scrape a JavaScript/Ajax-heavy site.
document.querySelector('textarea.sP3.sBFB').value = 'MY SUPER KEYWORDS';
You can't use elt.value on a textarea. Did you try with elt.textContent?
Why do you try to scrape the results. Google already creating a csv file for us.
Try downloading that. That links selector must be like $('.gux-combo gux-dropdown-c .sJK')
Will you use that for automating things ?
I'm not sure exactly what's happening here, but the classes that you're using for targeting are different for me. The OneBoxKeywordsInputPanel-input textarea that I assume you're attempting to target has a second class, sPFB, and no other classes. It's possible that these cryptic classes are dynamic in some way. I'd recommend using the more descriptive class names instead. The following works just fine for me:
document.querySelector('textarea.OneBoxKeywordsInputPanel-input')