how to make this translation function code by google apps-script? - javascript

I really want to add a translation menu on my google sheet but I definitely don't know what should I do at the last part.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Translation')
.addItem('ko to eng', 'myFunction')
.addToUi();
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
var trans = LanguageApp.translate(sheet,'ko','en');
that is it for now.
I want to make 'myFuncton' that can spread var 'trans' at the same place in the sheet.
how can I code this?

If you'd like to print the translation e.g. on cells to the right of the selected cells, you can use this function:
function myFunction () {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
var trans = range.getValues().map(row => [LanguageApp.translate (row,'ko','en')])
range.offset(0,1).setValues(trans)
}

Related

How do you make Text value equal a Variable value using google apps script in the google documents?

im making a system to edit the text as a viewer, but I can't figure it out!
Here's my code so far:
function onOpen(e) {
DocumentApp.getUi()
.createMenu('Check')
.addItem('Check', 'check')
.addToUi();
}
function check() {
var ui = DocumentApp.getUi();
var result = ui.prompt("Enter change text");
Logger.log(result.getResponseText());
Logger.getLog
var body = DocumentApp.getActiveDocument().getBody();
var qwerty = DocumentApp.insertText.insertText();
}
I was expecting it to add text, but it just gave me an error.

How to import data from Google sheets to Slides

I am trying to create a button in google sheets that run a script every time it is triggered. The script should first create a new copy of the template and then using that google sheet replace all placeholder text in that presentation with data from sheets. I am not sure why the code is not working as of now.
// The following creates the UI button in sheets (This works)
function onOpen() {
let ui = SpreadsheetApp.getUi();
ui.createMenu('Create Report')
.addItem('Create Report', 'executeAll')
.addToUi();
}
function executeAll (){
var reportTemplate = DriveApp.getFileById('Presentation ID goes Here');
var copiedTemplate = reportTemplate.makeCopy('New Report', DriveApp.getFolderById("Folder ID Goes here"));
var skeleton = SlidesApp.openById(copiedTemplate.getId());
var slides = skeleton.getSlides();
return slide1();
function slide1 (){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('App Script Input Sheet');
var data = sheet.getRange('E1:F26').getValues;
var slide1 = slides[0];
var newslide1 = slide1.duplicate();
var shapes = (newslide1.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{Date}}',data[2]);
shape.getText().replaceAllText('{{Title}}',data[3]);
shape.getText().replaceAllText('{{Value 1}}',data[4]);
shape.getText().replaceAllText('{{Value 2}}',data[5]);
//there are more to be replaced. will add once code works.
});
}
}

Get link to sheet in image

how can i go to a specific sheet when i click on the image? I need to go to the "FERMENTADOR 2_002 / 2021" sheet when the user clicks on the "fermenter" image.
the problem is that the name of the sheet may change according to some criteria ... so the sheet to be activated needs to be with the value of cell A4 + the value of cell B16
function hyperlink(){
var sheet = SpreadsheetApp.getActiveSheet();
var nomeferm = sheet.getRange("A4").getValue();
var numlote = sheet.getRange("B16").getValue();
var pagina = nomeferm+"_"+numlote;
var tt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(pagina);
}
Updated Code based on your comment:
function getToSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('HOME');
const name = sh.getRange('A4').getValue()+"_"+sh.getRange('B16').getValue();
const sheet = ss.getSheetByName(name);
if(sheet){
ss.setActiveSheet(sheet, true);
}
}
Solution:
I guess the most straightforward way is to attach the following very simple script to your image:
function getToSheet() {
const ss = SpreadsheetApp.getActive();
ss.setActiveSheet(ss.getSheetByName('FERMENTADOR 2_002/2021'), true);
}
The script will activate sheet FERMENTADOR 2_002/2021 therefore it will redirect you there.
You just need to assign this script to your image and then you can click on the image itself (see attached gif).
Illustration:

Issues with Triggers on Google Script

I have the following function which works perfectly fine when I run it manually.
function LastUpdate() {
var thisSS = SpreadsheetApp.getActiveSpreadsheet();
var sheet = thisSS.getSheetByName('Roster');
var data = Session.getActiveUser().getEmail();
var Edit = 0;
var time = TimeStamp();
var Row;
Row = findInColumn("L", data);
Edit = checkEdits(Row);
if (Edit == 1)
{
sheet.getRange(Row,20).setValue(time);
}
}
I set up a trigger manually by going to Resources -> Triggers and selected the above Function to execute onEdit. Basically, whenever a cell in the sheet is edited, I want to automatically trigger this function to execute. I am not sure what the issue is !
Any help would be much appreciated.
EDIT - I tried to do this and this also does not work
function onEdit(e)
{
LastUpdate ()
}
Why don't you use the simple onEdit trigger instead of the installed trigger? Since you're looking for the latest edit made by the active user that might be a much easier, faster and more stable solution.
function onEdit(e){
var range = e.range; //edited range
range.getColumn();
range.setValue();
//etc
}

Google Spreadsheet Custom Menu Returned Script Function Not Found

I am trying to add a custom menu to the google spreadsheet editor's interface to activate a script by Tony Hirst to pull calendar events into the spreadsheet.
By when I click on the custom menu option, it returned "script function not found: GetCalendar()"
I have tried to make modifications and read the google documentation but am not sure what I am doing wrong.
Thank you.
/**
* A custom function that gets the calendar events by calendar ID.
*/
function GetCalendar() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the second sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
//http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
// The code below will retrieve events between 2 dates for the user's default calendar and
// display the events the current spreadsheet
var calId = "SpecificCalendarID";
var cal = CalendarApp.getCalendarById(calId);
var events = cal.getEvents(new Date("June 15, 2015"), new Date("March 20, 2080"));
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var details=[[events[i].getTitle(), events[i].getDescription(), events[i].getStartTime()]];
var row=i+2;
var range=sheet.getRange(row,1,1,3);
range.setValues(details);
}
}
/**
* A function that runs when the spreadsheet is open, used to add a
* custom menu to the spreadsheet.
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menuItems = ui.createMenu ('Custom Menu');
menuItems.addItem ('Get Events','GetCalendar()');
menuItems.addToUi();
}
Remove the parenthesis from GetCalendar()
Should be:
menuItems.addItem ('Get Events','GetCalendar');

Categories

Resources