I wrote a function for using a spinText synonyms in docs. Ie. I have a text in the document like this: "{Hello|Hi} Mr {Thomas|Mathew|Andrew}"
Function give me 2 arrays: toreplace[], synonyms[] but the result of
for (var i = 0; i < rangeElements.length; ++i) {
body.replaceText(toreplace[i],synonyms[i]);
}
give me a text like this: Hi|Hi Mr Andrew|Andrew|Andrew but i would like to get "Hi Mr Andrew".
I tried Logger.log(toreplace[0]) and gets "{Hello|Hi}" and Logger.log(synonyms[0]) show "Hi" so it should be translated into body.replaceText('{Hello|Hi}','Hi');
What i am doing wrong?
function synonymize() {
var body = DocumentApp.getActiveDocument().getBody();
var rangeElements = [];
var rangeElement=null;
var start=[];
var end=[];
var lentabs=[];
var str;
var synonyms=[];
var toreplace=[];
var x=0;
while (rangeElement = body.findText('[{].+?[}]',rangeElement))
{
rangeElements.push(rangeElement);
start.push(rangeElement.getStartOffset());
end.push(rangeElement.getEndOffsetInclusive());
}
for (var i = 0; i < rangeElements.length; ++i) {
lentabs[i]=rangeElements[i].getElement().getText().substring(start[i]+1, end[i]).split('|').length
toreplace[i]=rangeElements[i].getElement().getText().substring(start[i], end[i]+1);
min=0;
max=lentabs[i]-1;
rand=Math.floor(Math.random()*(max-min+1)+min)
synonyms.push(rangeElements[i].getElement().getText().substring(start[i]+1, end[i]).split('|')[rand]);
}
for (var i = 0; i < rangeElements.length; ++i) {
body.replaceText(toreplace[i],synonyms[i]);
}
}
Ok. The problem was in the "|" which is treated as a regex pattern. I changed text in the document to "{Hello#Hi} Mr {Thomas#Mathew#Andrew}" and now it's working ok.
Related
In Google Sheets, I have a long list of names in column A, and tags in column B. What I want to accomplish with my function is to return only those values that I have stored in the functions list variable.
Currently I have a ReferenceError in my script project, and I don't know why.
Help would be appreciated.
function myFunction(cellInput) {
var input = cellInput;
var list = " word 1; word 2; word-3; word 4; word6; word-7;";
var splitted_input = input.split("; ");
var splitted_list = list.split("; ");
var result = "";
for (var inc1 = 0; inc1 < splitted_list.length; inc1++) {
for (var inc2 = 0; inc2 < splitted_input.length; inc2++) {
var resSet = new Set(result.split("; "));
if(splitted_list[inc1] == splitted_input[inc2] && !resSet.has(splitted_input[inc2])) {
result = result + splitted_input[inc2] + "; ";
}
}
}
return result;
}
I have AJAX/JSON operation that calls a PHP endpoint.
I'm getting data from database and need to set it to each field.
Here is the Ajax code:
$.ajax(
{
url: "loadEquipmentDetails",
type: "POST",
data:
{
equipmentID: equipmentIDValue
},
success: function(jsonStr)
{
var data = JSON.parse(jsonStr);
var item1Len = data.item1Name.length;
$('#equipmentIDFilter').val(data.equipmentID);
$('#equipmentNameFilter').val(data.equipmentName);
for(var i=0; i<item1Len; i++)
{
var item1Name = data[i].item1Name;
alert(item1Name);
var newOption = $('<option>baba</option>');
$('#item1IDFilter').append(newOption);
}
}
});
As you can see on above code, for this:
$('#equipmentIDFilter').val(data.equipmentID);
$('#equipmentNameFilter').val(data.equipmentName);
Working good, the value can be set to that ID.
But why the value is not show on below:
var newOption = $('<option>baba</option>');
$('#item1IDFilter').append(newOption);
I try to alert it alert(item1Name) but there is no result/pop up.
Response result:
There is a problem with the logic of your loop.
var item1Len = data.item1Name.length;
later on you have
for(var i=0; i<item1Len; i++)
{
var item1Name = data[i].item1Name;
alert(item1Name);
var newOption = $('<option>baba</option>');
$('#item1IDFilter').append(newOption);
}
which assumes that data is an array, and each item has a key item1Name
I could only make assumptions of how your data looks like but I imagine it would have to be something like
for(var i=0, iMax = data.options.length; i<iMax; i++)
{
if(data.options[i].hasOwnProperty('item1Name')) {
var item1Name = data.options[i].item1Name;
var newOption = $('<option>${item1Name}</option>');
$('#item1IDFilter').append(newOption);
}
}
UPDATE: after the json was added to the question
if (data.hasOwnProperty('item1Name')) {
for (var i = 0, iMax = data.item1Name.length; i < iMax; i++) {
var item1Name = data.item1Name[i];
var newOption = $('<option>${item1Name}</option>');
$('#item1IDFilter').append(newOption);
}
}
I'm trying to make a script for google sheet, who can count a letter in a text. But it seems that .length doesn't work. Anyone who can give directions on where to find the the solution.
function Tjekkode(tekst , bogstav){
var test = "";
// find the length of laengdeTekst
var laengdeTekst = tekst.length;
var t = 0;
// through the text character by character
for ( var i = 1; i<laengdeTekst ; i++) {
var test = tekst.substr(i,1);
if (test == bogstav) {
// if the letter is found, it is counted up
// REMEMBER == means compare
var t = t + 1;
}
}
// returns percent appearance of the letter
Return = t / længdeTekst * 100
}
Thanks in advance
length is ok in your code. To test it, run this script:
function test( ) {
var test = "123456";
// finder længden på teksten
var laengdeTekst = test.length;
Logger.log(laengdeTekst);
}
After you run it, check Log, press [Ctrl + Enter]
The correct code in your case:
function Tjekkode(tekst, bogstav) {
var test = "";
var laengdeTekst = tekst.length;
var t = 0;
// start looping from zero!
for ( var i = 0; i<laengdeTekst; i++) {
var test = tekst.substr(i,1);
if (test == bogstav) {
var t = t + 1;
}
}
// JavaScript is case sensitive: 'return != Return'
return t / laengdeTekst * 100;
}
Please, look at this tutorial for more info
thanks
I'll guess that I might get the one with the R instead of r at the end, but the script didn't run that line, because it kinda stopped at the .length line :/
the comments in danish is for my pupils (I'm a teacher in elementary school)
I'll see if google wants to cooperate today :|
This is the google script that worked for me. Note the 24 - that's the length of an empty message that has markup like <div>...</div>
function TrashEmptyDrafts() {
var thread = GmailApp.getDraftMessages();
for (var i = 0; i < thread.length; i++) {
b=thread[i].getBody();
if (b.length <= 24.0){
thread[i].moveToTrash();
}
}}
I want to go through all the Google groups I am a member of and get the list of all the users of each group :
var list = GroupsApp.getGroups();
for(var i = 0; i< list.length; i++){
for(var key in headerObj){
var text = "";
if(key == "users"){
var tab = list[i].getUsers();
if(tab.length > 0){
text = tab[0].getEmail();
for(var j = 1; j < tab.length; j++){
text += ", " + tab[j].getEmail();
}
}
headerObj[key].push(text);
}
}
}
But I always get this Exception :
You do not have permission to view the member list for the group: "group email"
Is there a way to go through all the Google groups of which, I am the administrator ?
Unfortunatly such a thing is not possible there is however the workaround of a try catch:
function myFunction() {
var allGroups = GroupsApp.getGroups();
for (var i in allGroups){
try {
var users = allGroups[i].getUsers();
for (var j in users){
Logger.log(users[j]);
}
}
catch (e) { }
}
}
I would like a question in my form to display the answer to a previous question. I am not asking about question logic or the Go To feature.
For example:
Q1: What type of entree would you like?
Meat
Fish
Vegetarian
Q2: Which cuisine do you prefer?
Indian
American
Mexican
Page break.
Q3: In the previous questions, you selected an [Q2] [Q1] entree.
If the answer to Q1 was Meat and to Q2 was Indian, then Q3 should display:
In the previous questions, you selected an Indian Meat entree.
This is possible in SurveyMonkey (with the use of square brackets). Can this be done in Google Forms?
I've written some dirty code in the boss´ time
It's a mix of server and client side script, I could have created the question Q3 in the function q1H & q2H.
var q1A = new Array(" Meat", " Fish", " Vegetarian")
var q2A = new Array(" Indian" ," American", " Mexican")
function doGet(e) {
var app = UiApp.createApplication().setTitle("Dev Test Sitemap");
var mainPanel = app.createVerticalPanel()
var homePanel = app.createHorizontalPanel()
var q1 = app.createLabel("Q1 What type of entree would you like?")
var q1ListBox = app.createListBox().setName("q1ListBox") ;
for (var i = 0; i < q1A.length; i++) {
q1ListBox.addItem(q1A[i]);
}
var handlerQ1 = app.createServerHandler("q1H");
q1ListBox.addChangeHandler(handlerQ1)
mainPanel.add(q1);
mainPanel.add(q1ListBox);
var q2 = app.createLabel("Q2 Indian American Mexican?")
var q2ListBox = app.createListBox().setName("q2ListBox") ;
for (var i = 0; i < q2A.length; i++) {
q2ListBox.addItem(q2A[i]);
}
var handlerQ2 = app.createServerHandler("q2H");
q2ListBox.addChangeHandler(handlerQ2)
mainPanel.add(q2);
mainPanel.add(q2ListBox);
mainPanel.add(app.createLabel("----------------------------------- pagebreak ---------------------") );
var horP = app.createHorizontalPanel()
//Q3: In the previous questions, you selected an [Q2] [Q1] entree.
var q3 = app.createLabel("In the previous questions, you selected an")
horP.add(q3)
var q2ans =new Array()
for (var i = 0; i < q2A.length; i++) {
q2ans[i] = app.createLabel(q2A[i]).setId("q2A"+[i]).setVisible(false);
horP.add(q2ans[i])
}
var q1ans =new Array() ;
for (var i = 0; i < q1A.length; i++) {
q1ans[i] = app.createLabel(q1A[i]).setId("q1A"+[i]).setVisible(false);
horP.add(q1ans[i])
}
var q32 = app.createLabel(" entree")
horP.add(q32)
mainPanel.add(horP);
app.add(mainPanel); //red
return app.close();
}
function q1H(e) {
var app = UiApp.getActiveApplication();
var q1ListBox = e.parameter.q1ListBox;
for (var i = 0; i < q1A.length; i++) {
if (q1ListBox == q1A[i] ){
app.getElementById("q1A"+[i]).setVisible(true);
}
}
return app;
}
function q2H(e) {
var app = UiApp.getActiveApplication();
var q2ListBox = e.parameter.q2ListBox;
for (var i = 0; i < q2A.length; i++) {
if (q2ListBox == q2A[i] ){
app.getElementById("q2A"+[i]).setVisible(true);
}
}
return app;
}
Working example