Google Slides Apps Script retrive a shape in a page - google-apps-script

how to retrieve a particular shape base on the text of the shape.
example: I would like to retrieve a shape in which text starts with "Issue" and get the content of the text to input it in Google Sheets
thanks for helping!

I believe your goal as follows.
You want to retrieve the texts from the shapes in Google Slides.
When the top of text is Issue, you want to retrieve the text, and want to put the retrieved texts to Google Spreadsheet.
You want to achieve this using Google Apps Script.
In this case, how about the following sample script? Unfortunately, from your question, I cannot understand about the output situation you expect. So the following sample script puts the retrieved values to the 1st column.
Sample script:
Please copy and paste the following script to the script editor. And, please set the variables and run myFunction.
function myFunction() {
const presentationId = "###"; // Please set the presentation ID (Google Slides ID).
const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
const searchText = "Issue";
// Check texts and retrieve texts from Google Slides.
const regex = new RegExp(`^${searchText}`);
const slides = SlidesApp.openById(presentationId).getSlides();
const values = slides.flatMap(slide => slide.getShapes().reduce((ar, shape) => {
const text = shape.getText().asString().trim();
if (regex.test(text)) ar.push([text]);
return ar;
}, []));
// Put the retrieved texts to Google Spreadsheet.
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
Note:
Unfortunately, I'm not sure whether above sample script is what you need. So when above script is not useful for your situation, in order to correctly understand about your goal, can you provide the sample Slides and the sample result you expect? By this, I would like to modify the script.
References:
getShapes()
getText()
setValues(values)

Related

How do I automatically turn a URL I input into a cell into a hyperlinked text in Google Sheets?

Please see image for reference. I want the cell to always read "Link" in columns B through E whenever any URL is posted in those cells. I was told this needs to be an Apps Script; however, I'm not exactly sure on how to go about doing this.
So as an example: I post a random URL, randomURL1.com, in cell B3 I want it to automatically change that to read "Link" and be a clickable text.
Thanks!
https://i.stack.imgur.com/ZYKbs.png
I believe your goal is as follows.
When an URL is put to a cell, you want to convert the URL to a text which has a hyperlink of the URL.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet and please set the sheet name. And, please save the script. When you use this script, please put an URL to a cell. By this, the URL is converted to a text of "Link" with the hyperlink.
function onEdit(e) {
const sheetName = "Sheet1"; // Please set the sheet name.
const { range } = e;
const sheet = range.getSheet();
const value = range.getValue();
if (sheet.getSheetName() != sheetName || !value.match(/^https?:\/\/.+/)) return;
const r = range.getRichTextValue().copy().setText("Link").setLinkUrl(value).build();
range.setRichTextValue(r);
}
For example, when an URL like https://sample.com is put to a cell, this script is run and a text of "Link" with the URL is put to the cell.
In this script, RichText is used. If you want to use a formula of HYPERLINK, please modify as follows.
From
const r = range.getRichTextValue().copy().setText("Link").setLinkUrl(value).build();
range.setRichTextValue(r);
To
range.setFormula(`=HYPERLINK("${value}","Link")`);
In this script, in order to check the URL, http:// or https:// are checked. So, please put the URL including http:// or https://.
References:
Simple Triggers
Class RichTextValueBuilder
setFormula(formula)

How can I get an image FROM A CELL in a Google Sheet and insert it into a Google Doc [duplicate]

I have a script that creates a document during runtime and attach it to a variable.
I need to insert images to it using a script.
Here is my code:
var modulo = "foo";
var nomeDoc = "bar";
let doc = DocumentApp.create("Validação escopo ("+ modulo +") cliente: " + nomeDoc);
var body = doc.getBody();
var imgPDF = body.appendImage(blob);
How do i pass an image as "blob" inside the variable: imgPDF?
Important: The image is in the Spreadsheet that calls this function.
On January 19, 2022, 2 Classes for using the inner cell image were added to the Spreadsheet service. Ref But, in the current stage, the image can be put into a cell. But, unfortunately, the image in the cell cannot be retrieved. I think that this might be a bug. And also, these Classes cannot retrieve the images on a cell as the blob and the image URL. I think that this is the specification.
So, as the current workaround, I thought that in your situation, in the current stage, this method can be used. Ref
In this workaround, a Google Apps Script library might be able to be used. Ref This library can retrieve both the image in a cell and the image on a cell.
Usage:
1. Install Google Apps Script library.
You can see the method for installing this library at here.
2. Enable Drive API.
In this case, Drive API is used. So, please enable Drive API at Advanced Google services.
3. Sample script.
const spreadsheetId = "###"; // Google Spreadsheet ID
const res = DocsServiceApp.openBySpreadsheetId(spreadsheetId).getSheetByName("Sheet1").getImages();
console.log(res); // You can check the retrieved images at the log.
if (res.length == 0) return;
const blob = res[0].image.blob; // Here, 1st image of Sheet1 is retrieved. Of course, you can choose the image on the sheet.
let doc = DocumentApp.create("Validação escopo (" + modulo + ") cliente: " + nomeDoc);
var body = doc.getBody();
var imgPDF = body.appendImage(blob);
In this case, please declare modulo and nomeDoc.
4. Testing.
When the above script is run, the images are retrieved from "Sheet1" and put the 1st image to the created Document body.
References:
DocsServiceApp
Related thread
How to access new 'in-cell-image' from google apps script?

Save a range of Google Sheet as image in Google Drive [duplicate]

I am trying to copy a range of cells of a specific Google spreadsheet as an image onto a Google slide. But I could barely find useful code. This is what I came up with, but I still cannot transfer the cell range into an image/png.
Goal: Insert the image stored just in a variable to a specific slide!
Any help is very much appreciated. Thank you.
function add_WSA(){
//Opening the Spreadsheet
var ss = SpreadsheetApp.openById("insertSpreadsheetID");
var range = ss.getRange("example!A1:F20");//in A1 Notation
//Conversion into an png image
var image = range.getAs('image/png');
//Opening the specific Slide (Nr. 3)
var slide = SlidesApp.openById("mySlidesID").getSlides()[2];
//Insertion of image
slide.insertImage(image);
}
Error: TypeError: range.getAs is not a function
at add_WSA(report:5:21)
PS: I am farely new to the community and to JavaScript. Please be patient. Every other help on a leaner or more efficient solution to the problem is very much appreciated. Thank you.
How about this answer?
Issue and workaround:
Unfortunately, in the current stage, the range object cannot be directly converted to the PNG format. So in this case, it is required to use a workaround. In this answer, as the workaround, I would like to propose to use Charts Service. When Charts Service is used, the range of Spreadsheet can be converted to an image blob.
Sample script:
function add_WSA(){
//Opening the Spreadsheet
var ss = SpreadsheetApp.openById("insertSpreadsheetID");
var range = ss.getRange("example!A1:F20");//in A1 Notation
//Conversion into an png image
// I modified below script.
const [header, ...values] = range.getDisplayValues();
const table = Charts.newDataTable();
header.forEach(e => table.addColumn(Charts.ColumnType.STRING, e));
values.forEach(e => table.addRow(e));
const image = Charts.newTableChart().setDataTable(table.build()).setDimensions(500, 500).setOption('alternatingRowStyle', false).build().getBlob();
//Opening the specific Slide (Nr. 3)
var slide = SlidesApp.openById("mySlidesID").getSlides()[2];
//Insertion of image
slide.insertImage(image);
}
Result:
When above script is run, the following sample result can be obtained.
Note:
Please use this script with enabling V8.
In this case, for example, when you want to change the font color, please use HTML code in each cell value.
Reference:
Charts Service

Print multiple data per ID in Google Sheet

In this spreadsheet, I have a sheet called Form Responses 1 that contains the students' data and a sheet called TEMPLATE that contains the template that needs to be printed per student. The problem is that I can only print the spreadsheet one at a time by changing the data in the C2 cell. Is there any way for me to print it quickly and easily to save time and effort?
In Google Cloud Print, I want it to say Page 1 = Student 1, Page 2 = Student 2, Page 3 = Student 3, and so on. Is it possible?
In your situation, how about creating a new Google Spreadsheet including each page? When this is reflected in a sample script, it becomes as follows.
Sample script:
function myFunction() {
const srcSs = SpreadsheetApp.getActiveSpreadsheet();
const sheet = srcSs.getSheetByName("TEMPLATE");
const values = sheet.getRange("C2").getDataValidation().getCriteriaValues()[0].getValues().flat().filter(String);
const dstSs = SpreadsheetApp.create("tempSpreadsheet");
values.forEach(v => {
sheet.getRange("C2").setValue(v);
SpreadsheetApp.flush();
const tempSheet = sheet.copyTo(srcSs);
const range = tempSheet.getDataRange();
range.copyTo(range, {contentsOnly: true});
tempSheet.getRange("B2:2").clear().clearDataValidations();
tempSheet.getDrawings().forEach(e => e.remove());
tempSheet.copyTo(dstSs);
srcSs.deleteSheet(tempSheet);
});
dstSs.deleteSheet(dstSs.getSheets()[0]);
}
When this script is run, a new Google Spreadsheet of tempSpreadsheet is created to the root folder. When you see it, each tab has each sheet without using the formulas. By this, you can print out the Spreadsheet by one manual process.
Note:
When I saw your sample Spreadsheet, /** #OnlyCurrentDoc */ is used in your script. When you use my proposed script, please remove /** #OnlyCurrentDoc */. By this, the scope of https://www.googleapis.com/auth/spreadsheets is automatically detected and used it. Please be careful about this.
This sample script is for your sample Spreadsheet. So when the spreadsheet is changed, this script might not be able to be used. Please be careful about this.
References:
create(name)
copyTo(spreadsheet) of Class Sheet
copyTo(destination) Class Range

Is there a function to convert a google doc to a google spreadsheet?

I need a way to transfer the data from a google document to be transferred over to a google spreadsheet (kind of backwards, I know). I need the line breaks in the document to be equivalent to starting a new cell right below. i.e. each line in the google doc has its own cell.
I've tried getting the body text from the google doc and setting the first cell as that variable but that only pastes the data in the single A1 cell (kind of want it to be similar to the way if you copy and paste doc body text into the A1 cell it will populate cells in the A column all the way down)
var body = openedFile.getBody().getText();
var newSpreadsheet = SpreadsheetApp.create('TEST').getId();
var testSpreadsheet = SpreadsheetApp.openById(newSpreadsheet);
testSpreadsheet.getRange('A1').setValue(bodyAll);
You want to put each paragraph of Document to the cells of Spreadsheet which was created in the script.
From your script, your Google Document has only texts. Your Google Document doesn't include tables, lists, images and so on.
If my understanding is correct, how about this modification?
Pattern 1:
In this pattern, body of var body = openedFile.getBody().getText() is splitted by \n. Then, the values are put to the created Spreadsheet.
Modifed script:
var openedFile = DocumentApp.openById("###"); // Please set Document ID here.
var body = openedFile.getBody().getText();
var bodyAll = body.split("\n").reduce(function(ar, e) {
if (e) ar.push([e]); // If you want to include the empty paragraph, please modify to ar.push([e]);
return ar;
}, []);
var newSpreadsheet = SpreadsheetApp.create('TEST');
newSpreadsheet.getSheets()[0].getRange(1, 1, bodyAll.length, bodyAll[0].length).setValues(bodyAll);
Pattern 2:
In this pattern, the paragraphs are retrieved from the body of Document, and each text is retrieved. Then, the values are put to the created Spreadsheet.
Modifed script:
var openedFile = DocumentApp.openById("###"); // Please set Document ID here.
var body = openedFile.getBody();
var bodyAll = body.getParagraphs().reduce(function(ar, e) {
var temp = e.getText();
if (temp) ar.push([temp]); // If you want to include the empty paragraph, please modify to ar.push([temp]);
return ar;
}, [])
var newSpreadsheet = SpreadsheetApp.create('TEST');
newSpreadsheet.getSheets()[0].getRange(1, 1, bodyAll.length, bodyAll[0].length).setValues(bodyAll);
Note:
In this script, the empty paragraphs are ignored. If you want to include the empty paragraphs, please modify the script like the comment.
References:
split()
getParagraphs()
reduce()
If above scripts didn't resolve your issue, I apologize. At that time, in order to correctly understanding your situation, can you provide a sample Document and a sample Spreadsheet of the result you want? Of course, please remove your personal information from them.