Titles in Google Sheets Charts - google-apps-script

When I first create a chart in Google Sheets, the Title is in a movable box that I can select and drag around inside the chart box.
If I then change that title programmatically, e.g.
chart = chart.modify()
.setOption('title',ChartTitleNameReplacement)
.build();
selectedSheet.updateChart(chart);
the Title jumps up to the top of the chart box and cannot be dragged anywhere.
What option setting do I need in the Google Apps Script code to keep that box movable? Alternatively, is there any way of specifying a position in the chart box for the Title?

I have experienced the same issue with your situation. I think that this might be a bug. In this case, I achieved to modify the title without locking the title position using Sheets API. The sample script is as follows.
Sample script:
Before you run the script, please enable Sheets API at Advanced Google services.
const updatedTitle = "### updated title ###";
const ss = SpreadsheetApp.getActiveSpreadsheet()
const ssId = ss.getId();
const chart = Sheets.Spreadsheets.get(ssId).sheets[0].charts[0];
delete chart.position;
chart.spec.title = updatedTitle;
Sheets.Spreadsheets.batchUpdate({requests: [{updateChartSpec: chart}]}, ssId);
In this case, the title of the 1st chart in the 1st sheet in the active Google Spreadsheet is modified.
Note:
When only the title is modified, an error like the internal error occurs. At UpdateChartSpecRequest, there is no fields. I think that this might be the reason of the issue. So I added the existing properties to the request body by modifying the title. Please be careful this.
In the current stage, when the title is updated, it seems the title position is reset.
This is a simple sample script for modifying the chart title without locking the title position. So please modify this for your actual situation.
I couldn't find this issue at the issue tracker. So how about reporting it?
References:
Advanced Google services
Method: spreadsheets.get
Method: spreadsheets.batchUpdate
UpdateChartSpecRequest

Related

Change hyperlink colors in Google Apps Script for Spreadsheets

EDIT: I have just spent an obscene amount of time trying to change the color for hyperlinks in Google Sheets, but it seems to be impossible.
I thought that by changing the RichTextValue it could be possible to alter the TextStyle of hyperlinks. But it doesn't seem to be possible to change only part of a cell's style.
Question: Am I right that it's impossible to change the style/color of hyperlinks in Google Sheets?
Original question
I want to change the default hyperlink color in a given Google Spreadsheet.
This is my attempt of coding a Container-Bound script:
function changeLinkColor() {
var app = SpreadsheetApp;
var spreadsheet = app.getActiveSpreadsheet();
var linkColor = app.newColor().setRgbColor("#B4EBF7").build();
spreadsheet.getSpreadsheetTheme().setConcreteColor(app.ThemeColorType.HYPERLINK, linkColor);
}
I can see that it apparently changes Logger.log(spreadsheet.getSpreadsheetTheme().getConcreteColor(app.ThemeColorType.HYPERLINK) to the desired value, but it makes no difference in my spreadsheet, hyperlinks are still blue.
I believe, however, that hyperlink colors in themes only work for reports, this is why it is making no difference for links inside cells.

Google Slides & Apps Script: Unlink all embedded sheet charts

I'm publishing Google Slides containing a lot of embedded charts coming from a Spreadsheet.
I would like to unlink the embedded charts (and thus avoid to get the update button when the data are updated in the spreadsheet).
If google proposes to update all elements at once through the "Linked objects" entry of the Tools menu, there is no option to unlink all in one shot. I would need to go on each chart and select unlink.
So I'm looking now the option of writing a Google Apps Script to do that without success.
I found a similar question on stackoverflow here:
Remove all hyperlinks of a Google Slide using GAS
But the removeLink function does not have any effect on my chart. I still see the chained icon on the top right corner.
Any idea ?
Unfortunately, it seems that in the current stage, there are no methods for directly removing the link to Spreadsheet from Speadsheet. But when the link to Spreadsheet from Speadsheet chart is removed, it is found that the object becomes an image. I thought that this might be used for achieving your goal. So, from this situation, as a workaround, I would like to propose the following sample script.
Sample script:
This sample script converts the Spreadsheet chart to an image on the 1st slide. By this flow, the link to Spreadsheet from Speadsheet chart is removed.
const slide = SlidesApp.getActivePresentation().getSlides()[0];
const charts = slide.getSheetsCharts();
const chart = charts[0];
slide.insertImage(chart.asImage().getBlob(), chart.getLeft(), chart.getTop(), chart.getWidth(), chart.getHeight());
chart.remove();
The flow of this script is as follows.
Retrieve the Spreadsheet chart.
Retrieve the image blob from the chart.
Insert the image blob by copying the size and place of the Spreadsheet chart.
Remove the Spreadsheet chart.
Note:
This sample script copies the size and place. When you want to copy other values, please modify the script.
References:
asImage()
insertImage(blobSource, left, top, width, height)
remove()
Added:
About your additional question as follows.
I think this is a good workaround for charts (such as pie charts, column ... that can be converted as images). Nevertheless, I have some slides where I have some cells embedded. Running this code on this element is displaying an issue. Do you think this also feasible on embedded tables?
The chart is different from the table. So in this case, I think that your additional question is new question.
Your initial question is for removing the Spreadsheet link from the chart. My answer is for your this question. In this case, the table cannot be used. And, in the current stage, unfortunately, there are no methods for removing the link of Spreadsheet from the table. And also, when the Spreadsheet link is removed from the table, the object type is not changed from the table. By this, my workaround cannot be used. But Slides service and Slides API are growing now. So I think that such method might be added in the future update.
So, as the current method, how about reporting your new question to Google issue tracker? By this, the addition of such method might be considered.

Add hyperlink to a table cell within a Google Slides presentation

In my Google slideshow, I have a table. The table updates every 15 minutes. I have cells in this table that I want to include hyperlinks. I tried to manually enter a hyperlink by highlighting the text in the cell and right clicking, selecting "link". But when the table updates the hyperlinks gets removed. I've tried both setText() and insertText() in the script, both erase the links.
I've also looked into having the link automatically be re-added once the update was done, however slideshow tables don't have an insertLink command. Also, as this table is not actually a Google Spreadsheet, there is no setFormula() command.
Here is what I'm currently using to update the table. Works, just need hyperlinks added to the text.
FYI the links will be to other files in the Google Drive, if that matters...
var slide = SlidesApp.getActivePresentation().getSlides()[slideNo];
var table = slide.getTables();
var cell = table[0].getCell(row,4);
var trafficTime = convertTime(res["routes"][0]["summary"]["travelTimeInSeconds"]);
var trafficDelay = Math.round(res["routes"][0]["summary"]["trafficDelayInSeconds"]/60);
var displayedTime = trafficTime + " +" + trafficDelay;
cell.getText().clear();
cell.getText().insertText(0,displayedTime);
You want to add a hyperlink to a text of a cell in a table on Google Slides.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this modification? Please think of this as just one of several possible answers.
Modification point:
In your case, TextRange returned from insertText can be used. For this, you can use the method of getTextStyle().setLinkUrl(URL).
Modified script:
When your script is modified, please modify as follows.
From:
cell.getText().insertText(0,displayedTime);
To:
var textRange = cell.getText().insertText(0,displayedTime);
textRange.getTextStyle().setLinkUrl(url);
or
cell.getText().insertText(0,displayedTime).getTextStyle().setLinkUrl(url);
In this casem, url is a string type like https://###sample###/.
References:
getTextStyle()
setLinkUrl(url)
If I misunderstood your question and this was not the direction you want, I apologize.

To copy the cell text and the comments to another sheet using google apps script?

I have a google spreadsheet with comments in each cell. Can i programatically read, copy and the cell text and the comments to another sheet using google apps script? I need this facility with comments and not with notes.
Just discovered, that .moveTo(target) is able to move comments even between sheets.
You can read about it here: https://developers.google.com/apps-script/reference/spreadsheet/range#movetotarget
Hope this helps somebody.
This function will move a cell with its comments to a different sheet.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Original");
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Target");
sheet.getRange("A1").moveTo(targetSheet.getRange("A1"));
}
There is currently no way to interact with commends for Google Apps script.
You can follow this bug to see how the implementation of that is coming along (not very fast).

Script to update Google Form from Spreadsheet

Here's my problem:
I can't seem to piece this info together nor find it anywhere even though it seems simple enough. When I find help, they just tell me to enter it manually (but my users can't be trusted) or make a new form (not an option).
What I need is to be able to keep:
the same google form
it's ID and link to the same spreadsheet
the same 20 "paragraph text" questions (titles remain the same).
There can't be any new questions or anything that would change the name or layout of the response destination page in the spreadsheet.
However, what I want is:
to update the help text below these questions from twenty cells in the spreadsheet. If my cells are from a range called questions!b2:b19 in the same spreadsheet, how do I use a script to take their contents and write over and update the help text in these paragraph questions on the google form?
Any help would be greatly appreciated.
It might be easiest to create your form in AppScript. With your form created, you can then use AppScript to access your spreadsheet. Read the information from the sheet and use it to create the help text on your questions with setHelpText.
Here's a basic sample.
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
var ss = SpreadsheetApp.openById("<ID>");
var val = ss.getRange(<RANGE>).getValue();
item.setTitle('Question');
item.setHelpText(val);
item.setChoices([
item.createChoice('ONE'),
item.createChoice('TWO'),
item.createChoice('THREE')
]);