I want to remove a first text on the cell - google-apps-script

I want to remove "/input " before the other text.
I use this code
else if(text.startsWith("/input")) {
var sheet = SpreadsheetApp.openById(ssId).getSheetByName("inputan");
var item = text.split(",");
sheet.appendRow([date,id,name,item[0],item[1],item[2],item[3]]);
sendText(id,"Thanks " + name + " data has been recorded");
}
For the text, I use code
var text = data.message.text;
I have tried to edit 'var text' but the other command come error
also I tried to separate the item, but only input number 1 comma 2 comma and 2,3, and 4 entered the table

I want to remove "/input " before the other text
Use String.replace(), like this:
const text = String(data.message.text).replace(/\/input /i, '');

Related

Adding words in bold to table using python-docx

I'm new here (and to python) so any feedback on my post is welcome.
I have some code which asks for an input and then adds it to an entry in various tables.
e.g
import docx
doc = docx.Document('ABC.docx')
length = len(doc.tables)
name = input("What is your name?")
x = range(0,length)
for r in x:
doc.tables[r].cell(0, 1).text = name + ": " + doc.tables[r].cell(0, 1).text
doc.save("ABC_.docx")
and this will take text like "I love you" and change it to "Bob: I love you", which is great. However, I'd like the Bob to appear in bold. How do I do that?
Not sure this is the perfect way to do this, but it works. Basically you store the current cell text in a variable then clear the cell. After that, get the first paragraph of the cell and add formatted runs of text to it, as follows:
import docx
name = input("What is your name?")
doc = docx.Document('ABC.docx')
length = len(doc.tables)
x = range(0,length)
for r in x:
celltext = doc.tables[r].cell(0, 1).text
doc.tables[r].cell(0, 1).text = ""
paragraph = doc.tables[r].cell(0, 1).paragraphs[0]
paragraph.add_run(name).bold = True
paragraph.add_run(": " + celltext)
doc.save("ABC_.docx")
Input:
What is your name?NewCoder
Result:

Convert multiline div text to table with Jquery

I have a comma separated value list with several lines, and want to create a table with four columns using jquery. I have created a jquery function, but I am unable to reproduce the
$(function(){
$('div').prepend('<table><thead><tr><th>Author</th><th>Title</th><th>Year</th><th>Status</th></tr></thead><tbody>');
$('div').html(function(){
var text = $(this).text();
var array = text.split(',');
var array2 = array[0].split('-');
var html = '<tr><td>'+ array2[0] + '</td><td>'+ array2[1] + '</td><td>' + array[1] + '</td><td>' + array[2] + '</td></tr>';
$(this).html(html);
})
$('div').append('</tbody></table>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
author - book,2010,good
author2 - book2,2011,good
author3 - book3,2011,bad
author4 - book4,2012,average
author5 - book5,2009,bad
author6 - book6,2008,good
</div>
The expected output:
<table><thead><tr><th>Author</th><th>Title</th><th>Year</th><th>Status</th></tr></thead>
<tbody>
<tr><td>author</td><td>book</td><td>2010</td><td>good</td></tr>
<tr><td>author2</td><td>book2</td><td>2011</td><td>good</td></tr>
<tr><td>author3</td><td>book3</td><td>2011</td><td>bad</td></tr>
<tr><td>author4</td><td>book4</td><td>2012</td><td>average</td></tr>
<tr><td>author5</td><td>book5</td><td>2009</td><td>bad</td></tr>
<tr><td>author6</td><td>book6</td><td>2008</td><td>good</td></tr>
</tbody>
</table>
The Jquery code doesn't work as I expect, I am stuck with this.
Thank you!
Break this down into smaller tasks by first getting the data processed and then once you have that figured out build the html from there.
You are missing a key step in splitting the line breaks to get your initial rows strings using text.split('\n').
Once you get those rows then map each row to split the other characters to get a sub array of strings for each row. Since none of this processing requires using elements you can do it in a sandbox until you get to where you can take string input and log a final array to console.
Once you have a data array then build the table rows. Note in your append() process you can't append closing tags as you would in a code editor. When you append html into the dom it creates full elements so include all closing tags in that html string
Working example:
const $div = $('#myDiv')
// include the closing tags in the html string
const $table = $('<table><thead><tr><th>Author</th><th>Title</th><th>Year</th><th>Status</th></tr></thead><tbody></tbody></table>');
let txt = $div.text().trim();// trim to remove whitespace at each end
// split line breaks for rows array
let rows = txt.split('\n').map(s => {
// split the two parts
const [author, content] = s.split(' - ');
// return final sub array
return [author, ...content.split(',')]
});
// inspect your work so far
console.log('rows array',JSON.stringify(rows))
// now that dat works, insert into table
rows.forEach(row =>{
var $row = $('<tr>');
row.forEach(s=> $row.append($('<td>',{text:s})));
$table.find('tbody').append($row)
})
// finally insert the table
$('div').html($table)
td, th{ border: 1px solid #ccc}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myDiv">
author - book,2010,good
author2 - book2,2011,good
author3 - book3,2011,bad
author4 - book4,2012,average
author5 - book5,2009,bad
author6 - book6,2008,good
</div>

Trouble Adding Array output to an Dynamically Generated HTML String in GAS Google Script

I am trying to automate my businesses blog. I want to create a dynamic html string to use as a wordpress blog description. I am pulling text data from email body's in my gmail account to use as information. I parse the email body using the first function below.
I have everything working properly except for the for loop (in the second code block) creating the description of the post. I have searched for hours and tried dozens of different techniques but I cant figure it out for the life of me.
Here is how I am reading the text values into an array:
function getMatches(string, regex, index) {
index || (index = 1); // default to the first capturing group
var matches = [];
var match;
while (match = regex.exec(string)) {
matches.push(match[index]);
}
return matches;
}
This is how I am trying to dynamically output the text arrays to create a basic HTML blogpost description (which I pass to xmlrpc to post):
var1 = getMatches(string, regex expression, 1);
var2 = getMatches(string, regex expression, 1);
var3 = getMatches(string, regex expression, 1);
var3 = getMatches(string, regex expression, 1);
var fulldesc = "<center>";
var text = "";
for (var k=0; k<var1.length; k++) {
text = "<u><b>Var 1:</u></b> " + var1[k] + ", <u><b>Var 2:</u></b> " + var2[k] + ", <u><b>Var 3:</u></b> " + var3[k] + ", <u><b>Var 4:</u></b> " + var4[k] + ", <br><br>";
fulldesc += text;
}
fulldesc += "</center>";
Lastly here is the blog post description code (using GAS XMLRPC library):
var fullBlog = "<b><u>Headline:</u> " + sub + "</b><br><br>" + fulldesc + "<br><br>General Description: " + desc;
var blogPost = {
post_type: 'post',
post_status: 'publish', // Set to draft or publish
title: 'Ticker: ' + sub, //sub is from gmail subject and works fine
categories: cat, //cat is defined elsewhere and works fine
date_created_gmt: pubdate2, //defined elsewhere (not working but thats another topic)
mt_allow_comments: 'closed',
description: fullBlog
};
request.addParam(blogPost);
If there's only one value in the var1,2,3,4 arrays all works as it should. But any more than 1 value and I get no output at all from the "fulldesc" var. All other text variables work as they should and the blog still gets posted (just minus some very important information). I'm pretty sure the problem lies in my for loop which adds the HTML description to text var.
Any suggestions would be greatly appreciated, I'm burned out trying to get the answer! I am a self taught programmer (just from reading this forum) so please go easy on me if I missed something stupid :)
Figured it out: It wasnt the html/text loop at all. My blogpost title had to be a variable or text, but not both.
Not working:
title: 'Ticker: ' + sub, //sub is from gmail subject and works fine
Working:
var test = 'Ticker: ' + sub;
//
title:test,

Add a "/" between each word in a String

I've got this string :
var str:String = mySharedObject.data.theDate;
where mySharedObject.data.theDate has some words (not always the same words has it depends on which button the user clicked).
So mySharedObject.data.theDate = "words words words".
Is it possible to add a "/" between each word ? (without knowing which words are in mySharedObject.data.theDate).
In order to have:
mySharedObject.data.theDate = "words/words/words".
Edit : You can replace " " with "/" in your string, this will split string with " " separator and then join with "/"
mySharedObject.data.theDate= mySharedObject.data.theDate.split(" ").join("/")
You can also do that using String.replace() with a little regular expression which will replace all spaces (notice here the g (global) flag to replace all instances), like this :
var s:String = 'word word word';
trace(s.replace(/\s/g, '/')); // gives : word/word/word
And for more about regular expressions take a look here.
Hope that can help.

Remove all but the first word from a sentence

I need to find a way to take a sentence and remove all its words besides the first.
If the sentence is "Hi my name is dingo"
I need to get only the word "Hi"
var sentence : String = "Hi my name is dingo"
var words : Array = sentence.split( " " );
var firstWord : String = words[ 0 ];
trace( firstWord ) // outputs "Hi"
Obviously this only works for simple sentences without punctuation. If you need more complex word parsing you can use regexp:
var pattern : RegExp = new RegExp( "\\b.(\\w*).\\b",'gi' );
var words : Array = sentence.match( pattern );