Maybe someone can help me with my Problem.
First some background: I have copied some WhatsApp chats to Google Sheets. I use Latex to generate a book containing the chats. Google Sheet is able to display all Emojis from WhatsApp, Latex, of course, isn't. So I downloaded the Emojis as png files and defined Latex commands to include those emojis as graphic.
For Example: Too display the regular smiling emoji I type \grin. I have a list with hundreds of emojis in one row and the corresponding command in the next row.
Until now I used search and replace -> replace all in the same sheet for every single type of emoji. But as this takes hours I wondered if there is any way to make this more effective.
Here is a spreadsheet with a small example: Google Sheet
Thanks in advance!
You want to replace the Emojis to the defined Latex commands.
For example, you want to replace as follows.
From 03.01.19, 00:29 - me: Hi 😊 to 03.01.19, 00:29 - me: Hi \nettnett.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Flow:
The flow of this sample script is as follows. This sample script uses your shared Spreadsheet.
Retrieve data from the sheet of Codes for Smileys.
Create the request body for the findReplace request of batchUpdate method of Sheets API.
Run the method of batchUpdate.
Sample script:
This script used Sheets API. So, before you run the script, please enable Sheets API at Advanced Google services.
function myFunction() {
var dataSheet = "Codes for Smileys";
var sourceSheet = "unedited Chats with Smileys";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data = ss.getSheetByName(dataSheet).getDataRange().getValues();
data.shift();
var sheetId = ss.getSheetByName(sourceSheet).getSheetId();
var requests = data.map(function(row) {return {findReplace: {sheetId: sheetId, find: row[0], replacement: row[1]}}});
Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
}
This sample script uses your shared Spreadsheet. So in this case, the data sheet is Codes for Smileys. And the source sheet for converting is unedited Chats with Smileys.
Note:
If you change the sheet name, also please modify above script. Please be careful this.
When you run the script for the first time, the authorization screen is opened. So please authorize the scopes for using the script.
References:
Advanced Google services
spreadsheets.batchUpdate
FindReplaceRequest
Added:
You want to put the converted values to the sheet of Chat with Latex Code.
The sample script for achieving above is as follows.
Sample script:
function myFunction2() {
var dataSheet = "Codes for Smileys";
var sourceSheet = "unedited Chats with Smileys";
var destinationSheet = "Chats with LaTeX Codes";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(dataSheet);
var data = ss.getSheetByName(dataSheet).getDataRange().getValues();
data.shift();
var srcSheet = ss.getSheetByName(sourceSheet);
var tempSheet = srcSheet.copyTo(ss);
var sheetId = tempSheet.getSheetId();
var requests = data.map(function(row) {return {findReplace: {sheetId: sheetId, find: row[0], replacement: row[1]}}});
Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
var tempValues = tempSheet.getDataRange().getValues();
var destSheet = ss.getSheetByName(destinationSheet);
destSheet.getRange(destSheet.getLastRow() + 1, 1, tempValues.length, tempValues[0].length).setValues(tempValues);
ss.deleteSheet(tempSheet);
}
In this sample script, the following flow is run.
Copy the source sheet unedited Chats with Smileys as a temporal sheet.
Create request body for the batchUpdate method to the temporal sheet.
Run the batchUpdate.
Copy the converted values from the temporal sheet to the destination sheet Chats with LaTeX Codes.
In this case, the converted values are put to the last row of the sheet.
Delete the temporal sheet.
Related
I'm trying to get the data from this csv file: "https://app.matrixify.app/files/vasasro/99320ce64bfe22f855ce03e71796e4db/MatrixifyAllExport.csv"
This is for product that we want to edit for vasaro.com and we need to export the data to Google Sheets automatically.
Thank you for your help.
When I tried IMPORTDADA, it worked for small files, but not for this larger file.
When I check the CSV data of your URL, it seems that the data size is 2,413,638 bytes and 1158 rows and 93 columns. I think that this is the reason for your current issue. In this case, in order to put the values into Spreadsheet, how about using Google Apps Script?
Sample script 1:
Please copy and paste the following script to the script editor of Spreadsheet, and save the script. When you use this script, please put a custom function of =SAMPLE("https://app.matrixify.app/files/vasasro/99320ce64bfe22f855ce03e71796e4db/MatrixifyAllExport.csv") to a cell. By this, the CSV data is retrieved and put the values to the Spreadsheet. This can be used like IMPORTDATA.
const SAMPLE = url => Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText());
Sample script 2:
Please copy and paste the following script to the script editor of Spreadsheet, and save the script. And, please set the sheet name. In this case, please run the function myFunction with the script editor. By this, the CSV data is downloaded and put to the Spreadsheet.
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const url = "https://app.matrixify.app/files/vasasro/99320ce64bfe22f855ce03e71796e4db/MatrixifyAllExport.csv"; // This is from your URL.
const res = UrlFetchApp.fetch(url);
const values = Utilities.parseCsv(res.getContentText());
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
Sample script 3:
If the CSV data is large, when Sheets API is used, the process cost can be reduced a little. In that case, please enable Sheets API at Advanced Google services. And, please set the sheet name. And, please run the function myFunction with the script editor. By this, the CSV data is downloaded and put to the Spreadsheet.
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const url = "https://app.matrixify.app/files/vasasro/99320ce64bfe22f855ce03e71796e4db/MatrixifyAllExport.csv"; // This is from your URL.
const res = UrlFetchApp.fetch(url);
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheetId = ss.getSheetByName(sheetName).getSheetId();
const requests = [{ pasteData: { data: res.getContentText(), delimiter: ",", coordinate: { sheetId } } }];
Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
References:
Custom Functions in Google Sheets
parseCsv(csv)
setValues(values)
PasteDataRequest
I'm trying to Unlink all hyperlinks in a Google Spreadsheet, so far I'm trying to do a search and replace which is the code below, but wondering if there is a specific function to do the unlink?
function replacelinks(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Links");
var values = sheet.getDataRange().getValues();
replaceInSheet(values, '.com', '');
replaceInSheet(values, '.net', '');
I believe your goal as follows.
You want to remove all hyperlinks on a sheet in a Google Spreadsheet using Google Apps Script.
For this, how about this answer?
Modification points:
Unfortunately, in the current stage, it seems that there are no methods for directly removing the hyperlinks of the cells and texts. So in order to achieve your goal, I would like to propose a workaround. The flow of this workaround is as follows.
Copy the sheet you want to remove the hyperlinks in the Google Spreadsheet as a temporal sheet.
Retrieve the richTextValues from the data range of the source sheet.
Retrieve the cell coordinates, which have the hyperlinks, and create an object for using the method of batchUpdate in Sheets API.
Request to the batchUpdate method using the created object.
The cells with the hyperlinks on the source sheet are cleared, and the values of the same coordinates are copied from the temporal sheet to the source sheet. By this, the hyperlinks are removed.
Sample script:
Please copy and paste the following script to the script editor. And, please enable Sheets API at Advanced Google services. And please set the sheet name you want to remove the hyperlinks.
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const sheetId = sheet.getSheetId();
const range = sheet.getDataRange();
const temp = ss.insertSheet();
const tempId = temp.getSheetId();
range.copyTo(temp.getRange("A1"), {contentsOnly: true});
const requests = range.getRichTextValues().reduce((ar, row, i) => {
row.forEach((col, j) => {
const runs = col.getRuns();
if (col.getLinkUrl() || (runs && runs.some(e => e.getLinkUrl()))) {
const req1 = {updateCells:{range:{sheetId:sheetId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},fields:"userEnteredValue"}};
const req2 = {copyPaste:{
source:{sheetId:tempId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},
destination:{sheetId:sheetId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},
pasteType:"PASTE_NORMAL"
}};
ar = ar.concat(req1, req2);
}
});
return ar;
}, []);
Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
ss.deleteSheet(temp);
}
In this script, the hyperlinks of both with and without HYPERLINK() can be removed.
Note:
In the current stage, it seems that getRichTextValues() cannot retrieve the number values. By this, when the number values has the hyperlinks, these hyperlinks cannot be removed. About this issue, I have already reported to the issue tracker. Ref So when this issue was removed, I think that using above script, all hyperlinks might be be able to be removed. Or, in the current stage, the method of getLinkUrl() used in above script is not included in the official document. So I also think that the method for directly removing the hyperlinks might be added in the future update.
References:
getRichTextValues()
Advanced Google services
Method: spreadsheets.batchUpdate
UpdateCellsRequest
CopyPasteRequest
What helped for me was to setShowHyperlink to false for the range.
Example:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getActiveSheet();
const range = ws.getRange(lastRow, 1)
range.setValue('https://example.com');
range.setShowHyperlink(false);
But as soon as you double click such a cell, Google will re-apply the hyperlink.
I am trying to import fivethirtyeight data, but when trying to import the full link from the CSV file, it exceeds the limit allowed by google.
So ... I would like to know how I could filter so that only today's games and future dates are imported. From the games of past dates there is no value to me.
=IMPORTDATA("https://projects.fivethirtyeight.com/soccer-api/club/spi_matches.csv")
IMPORTDATA didn't work because it exceeds the spreadsheet limit, because it saves past game data since 2016, so the data list gets too big.
I just need the current date and future date games
How about this answer?
It seems that the size of CSV data is 4,183,375 bytes, and 32,101 rows and 22 columns. So how about putting the data using Google Apps Script? Unfortunately, this CSV cannot be put using the custom function, because of the large size.
From the benchmark for importing CSV data to Spreadsheet, when Sheets API is used, the process cost can be reduced from the method with Utilities.parseCsv() and setValues(). So in this answer, Sheets API is used for putting CSV values to Spreadsheet. Please think of this as just one of several answers.
Sample script 1:
Before you use the following scripts, please enable Sheets API at Advanced Google services.
function myFunction() {
var url = "https://projects.fivethirtyeight.com/soccer-api/club/spi_matches.csv";
var data = UrlFetchApp.fetch(url).getContentText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var resource = {requests: [{pasteData: {
data: data,
coordinate: {sheetId: sheet.getSheetId()},
delimiter: ","
}}]};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
}
In this script, the CSV data is put from the cell "A1" of the active sheet.
Sample script 2:
If you cannot use Sheets API, you can also use the following script.
function myFunction() {
var url = "https://projects.fivethirtyeight.com/soccer-api/club/spi_matches.csv";
var data = UrlFetchApp.fetch(url).getContentText();
var csv = Utilities.parseCsv(data);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csv.length, csv[0].length).setValues(csv);
}
References:
Benchmark: Importing CSV Data to Spreadsheet using Google Apps Script
PasteDataRequest
If this was not the direction you want, I apologize.
I'm trying to compare different versions of Spreadsheet data and highlight differences between the two versions. I know there is Version History feature, but what I'm trying is to get all the revision history when choosing the version.(i.e. if the latest version is five and I choose two, I want to get all the revisions made between version two and five).
I know how to access to the current version, but I'm stuck with how to do this with older versions. Is there any way to access to older versions as the same manner as the following code?
var spreadsheet = SpreadsheetApp.openById(id);
var sheet = spreadsheet.getSheets()[0];
var sheetdata = sheet.getDataRange().getValues();
You want to retrieve the values from other version of Spreadsheet using the following script.
var spreadsheet = SpreadsheetApp.openById(id);
var sheet = spreadsheet.getSheets()[0];
var sheetdata = sheet.getDataRange().getValues();
You have already got the revision ID of the Spreadsheet.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? When only values are retrieved from the other version of Spreadsheet, how about this workaround? Unfortunately, the Spreadsheet of the some revision cannot be directly retrieved as the Spreadsheet. So in this workaround, it is converted to other format and retrieved the values. Please think of this as just one of several answers.
Flow:
The flow of this workaround is as follows.
Retrieve Spreadsheet of other version as the excel format.
Convert the excel format to Google Spreadsheet.
By this, the values are separated to each sheet.
Retrieve values using your script.
Sample script:
Before you run this script, please enable Drive API at Advanced Google services. And set the variables of spreadsheetId and revisionId.
function myFunction() {
var spreadsheetId = "###"; // Please set the Spreadsheet ID.
var revisionId = "###"; // Please set the revision ID.
var url = "https://docs.google.com/spreadsheets/export?id=" + spreadsheetId + "&revision=" + revisionId + "&exportFormat=xlsx";
var blob = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}}).getBlob();
var id = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: revisionId}, blob).id;
var spreadsheet = SpreadsheetApp.openById(id);
var sheet = spreadsheet.getSheets()[0];
var sheetdata = sheet.getDataRange().getValues();
}
Note:
In this sample script, a Spreadsheet of the version is created to the root folder.
The filename is the revision ID.
When you want to retrieve the revision IDs of the Spreadsheet, please use Revisions: list.
References:
Download and publish file revisions
Revisions: list
Revisions: get
Files: insert
Advanced Google services
If I misunderstood your question and this was not the direction you want, I apologize.
Google sheets allows charts to be moved to their own sheet. Google scripts appears to be unable to rename these sheets though, without crashing the spreadsheet and forcing it to reload.
To see what I mean try this:
1) create new spreadsheet and put some data in it.
2) create any type of chart w/ the data.
3) edit the chart and select "Move To Own Sheet..."
4) create a script w/ the following code:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
sheets[1].setName('bob');
}
5) run the script and the spreadsheet crashes with the message: "Unable to Load File"
6) reload the spreadsheet and notice that the sheet did get renamed.
Why does the spreadsheet crash? Is it possible to rename the sheet w/o crashing?
Unfortunately, I couldn't find the clear reason of the crash. But I think that there is a workaround for avoiding the crash. So how about this workaround? In this workaround, it uses Sheets API. In order to use Sheets API, please enable Sheets API at Advanced Google Services and API console. About the how to enable them, please check here.
The sample script using Sheets API is as follows.
Sample script :
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var id = ss.getId();
var sheetId = ss.getSheets()[1].getSheetId();
var resource = {"requests":[{"updateSheetProperties":{"properties":{"sheetId":sheetId,"title":"bob"},"fields":"title"}}]};
Sheets.Spreadsheets.batchUpdate(resource, id);
}
If this was not what you want, I'm sorry.