Reputation: 23
The script: How to find and replace special characters in Google Doc using Apps Script?
function main() {
const doc = DocumentApp.getActiveDocument();
const replaces = [
{what: 'AM', to: 'am'},
{what: '\\.\\.', to: '.'},
{what: ':00', to: ''},
]
replaces.forEach(replace => doc.replaceText(replace.what, replace.to));
}
**Hello or looked at its script, the second version because new app script does not accept var. My problem is more complicated, but I can't find a solution, I'm not an expert and I can't speak English, I only know technical cse. On word I had created macros and I wanted to be able to use them on doc google with app script maybe with execution of manual triggers. The first is I need to look for all the string in the docs, which has both letters symbols etc, and is located between linettes, and replace the linettes with corporals, without touching the internal text former:
1 EX:"un’indagine veloce’ 345a qualcosa di più 44 sostanziale, come ‘facciamo un’indagine seria"
Resukt: «un’indagine veloce’ 345a qualcosa di più 44 sostanziale, come ‘facciamo un’indagine seria’»**
2 Select all words ll inside a list, and convert them to italics
3
maiusinz Macro
' Dim strText As String
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="[A-Z]{2;}", MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
With Selection
strText = Left(.Range.Text, 1) & LCase(Mid(.Range.Text, 2))
.Range.Text = strText
.Collapse wdCollapseEnd
.MoveRight wdCharacter, 1, False
End With
the third operation, I also attach vba scheme of word, and to find the words in round brackets and change the first letter to uppercase EX: (PDA) to (Pda) the text in parentheses is not fixed can be any if you can help me I'm grateful **
Upvotes: 1
Views: 509
Reputation: 14537
It could be something like this:
function main() {
const doc = DocumentApp.getActiveDocument();
// main
fix_quotemarks();
fix_cases(['PDA', 'AAA', 'BBB']);
set_italics(['qualcosa','seria']);
// functions
function fix_quotemarks() {
const replaces = [
{what: '“', to: '«'}, // <-- updated line
{what: '”', to: '»'}, // <-- updated line
{what: '"', to: '»'},
{what: ' »', to: ' «'},
{what: '^»', to: '«'},
]
replaces.forEach(replace => doc.replaceText(replace.what, replace.to));
}
function fix_cases(words) {
words.forEach(r => doc.replaceText(r, r[0]+r.slice(1).toLowerCase()));
}
function set_italics(words) {
var style = { [DocumentApp.Attribute.ITALIC]: true };
var pgfs = doc.getParagraphs();
for (var word of words) for (var pgf of pgfs) {
var location = pgf.findText(word);
if (!location) continue;
var start = location.getStartOffset();
var end = location.getEndOffsetInclusive();
location.getElement().setAttributes(start, end, style);
}
}
}
Before:
After:
Update (changed)
Here is the new function to convert case (AAA) --> (Aaa)
for any text inside the brackets:
function fix_cases_for_any_text_in_brackets() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var words = body.getText().match(/\(.+?\)/g) || [];
words = [...new Set(words)]; // remove duplicates
var replaces = words.map(r => ({
what: '\\(' + r.slice(1,-1) + '\\)',
to: '(' + r[1].toUpperCase() + r.slice(2,-1).toLowerCase() + ')',
}));
replaces.forEach(replace => body.replaceText(replace.what, replace.to));
}
Update 2*
Here is the function to convert all th 'AllCaps' words: AAAA --> Aaaa
:
function fix_AllCaps() {
const doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var words = body.getText().match(/[A-Z]{2,10}/g) || [];
words = [...new Set(words)]; // remove duplicates
var replaces = words.map(r => ({
what: r,
to: r[0].toUpperCase() + r.slice(1).toLowerCase(),
}));
console.log(replaces);
replaces.forEach(replace => body.replaceText(replace.what, replace.to));
}
Upvotes: 0