Basically what I'm trying to do is have google sheets take the user input and plug it into a website template that will then be used to scrape information from that website.
Example would be:
User selects "candy" from cell A2
I would then like to have another cell develop the website: www.something.com/candy/secondpart
This should work. You'll need to create some sort of trigger to make this happen, but this will return the HTML content.
function pullDateFromWebsiteBasedOnCell() {
const theSS = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //or specify
const theFirstPart = "https://www.something.com/";
const theLastpart = "/secondPart/";
var theMiddlepart = theSS.getRange("A2").getValue();
var theURL = theFirstPart+theMiddlepart+theLastpart;
var response = UrlFetchApp.fetch(theURL);
var theHTMLtext = response.getContentText(); //gets content as HTML blob.
Logger.log(theHTMLtext);
return theHTMLtext;
}
Related
I have read everything I could find on this site and others about methods of getting text out of a google sheet and into a google doc. I don't want the "data" in a table. It's not a huge amount of data to work with, so I'm able to tinker with the source sheet a bit and found a solution using a ListItem append approach.
Here's what I've come up with:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Agenda')
.addItem('CreateAgenda', 'createagenda')
.addToUi();
}
function createagenda() {
// take info from agenda topic google spreadsheet and put in a google doc
var documentId=DriveApp.getFileById('_______________').makeCopy().getId();
//add today's date to new filename
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var theDate = curr_month + "_" + curr_date + "_" + curr_year;
DriveApp.getFileById(documentId).setName(theDate + '_CommitteeAgenda');
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AgendaPlanning");
//Determine how many items are in the list to set count in For Loop
sheet.getRange('B2').activate(); // Starting cell in column with agenda items
sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var last_row_submissions = sheet.getCurrentCell().getRowIndex();
const body = DocumentApp.openById(documentId).getBody();
for (var i=2; i<= last_row_submissions; i++){
var currentline = sheet.getRange(i,14).getValue();
var agenda_item = body.appendListItem(currentline);
}
var documentsInPacket = sheet.getRange("B28").getValue();
var doclist = body.appendListItem(documentsInPacket);
}
For the most part this works because items on the agenda document are numbered, but because I've used the ListItem approach, the final item (Documents) has a number to it (18) which I don't want. Since someone has to go in and put in meeting info (zoom link, etc.) then having one more thing to do isn't horrible, but....
Minutes (6:30)
Commentary (6:35)
Adjourn (6:40)
Documents: Superintendent report; .....etc.
? Is there an easier way to do this? Can I get rid of the number preceding the last item? How can you get the value of a cell from sheets and have google doc accept it as text?
Thank you!
You can use body.appendParagraph(documentsInPacket) instead of body.appendListItem(documentsInPacket) to achieve the results you are looking for.
So I can get the xpath working for "mainstream" stocks like CVNA with this line:
=REGEXEXTRACT(INDEX(IMPORTXML("https://finance.yahoo.com/quote/CVNA?p=CVNA";"//*[#id='quote-header-info']");;3); "\d+.\d+|\d+")+0
But when trying to point to a specific exchange like Oslo exchange I get an error.
Would like to scrape stock prices from NEL.OL
Any suggestions?
As the page is built in javascript on the client side and not on the server side, you will not be able to retrieve the data by the importxml / importhtml functions. However, the page contains a json which you can retrieve and analyze to retrieve the information you need.
var source = UrlFetchApp.fetch(url).getContentText()
var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
var data = JSON.parse(jsonString)
for instance, for marketprice, you can retrieve the informations by
function marketPrice(code) {
var url='https://finance.yahoo.com/quote/'+code
var source = UrlFetchApp.fetch(url).getContentText()
var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
var data = JSON.parse(jsonString)
var regularMarketPrice = data.context.dispatcher.stores.StreamDataStore.quoteData[code].regularMarketPrice.raw
return regularMarketPrice
}
https://docs.google.com/spreadsheets/d/1sTA71PhpxI_QdGKXVAtb0Rc3cmvPLgzvXKXXTmiec7k/copy - column G
Is there a way to take the Title Text from the Google Sheets cell?
So whenever I change the cell string then it should appear in the Chart automatically.
For example: I have May - 2021 in cell A1. Can I reference this in Title Text, like using =A1 or =Sheet1!A1? But it does not work.
I got this code, but how do I reference the cell into the code?
function onOpen() {
SpreadsheetApp.getUi().createMenu('Charts').addItem('Update', 'myFunction').addToUi();
}
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var chart = sheet.getCharts()[0];
chart = chart.modify()
.setOption('Summary!Q123', sheet.getActiveCell().getValue() || 'Summary!Q123')
.build();
sheet.updateChart(chart);
}
Although it is not possible to link a cell value as title, there is a trick that may solve your problem by creating a merge cell, camouflage, as the title. Then it will be changed, based on the text value you enter :)
Script:
You may use the following script to update the title by referring to A3 every time you execute the code. Basically, you are almost correct:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var newtitle = sheet.getRange(3, 1).getValue();
var charts = sheet.getCharts()[0];
var chart = charts.modify()
.setOption('title', newtitle)
.build()
sheet.updateChart(chart)
var scnCharts = sheet.getCharts()[1];
var chart2 = scnCharts.modify()
.setOption('title', newtitle)
.build()
sheet.updateChart(chart2)
}
So I am new to Google Sheets. I am making an inspection report for a small company using Sheets. I would like to add a button to the bottom of the sheet which does the following script:
Duplicate original sheet
Rename sheet to a cell value +today's date. i.e Fred01011980
Email the sheet as a PDF to a recipient.
Finally clear the inputted values in the original master template sheet.
I have looked up how to do each of these and a few of them are straightforward, but I don't know how to combine them. Can I just add all of the functions individually together without any additional syntax needed? Any help on this would be really appreciated. Thank you.
I believe you can just create another function that calls your functions, like this:
function doAllStuff() {
doStuff();
doMoreStuff();
}
Here's how I did it:
function AllinOne() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = "Inspection";
var sheet = ss.getSheetByName("Inspection").copyTo(ss);
var newname = ss.getSheetByName("Inspection").getRange(2, 2).getValue();
var newnamedate = ss.getSheetByName("Inspection").getRange(3, 2).getValue();
var sheetx = ss.getActiveSheet()
var thisSheet = sheetx.getName();
var actualSheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName()
var sheetfinal = ss.getSheetByName("Inspection")
var old = ss.getSheetByName(newname+newnamedate);
if (old) ss.deleteSheet(old);
sheet.setName(newname+newnamedate); //copies the sheet and renames it to "store+date"
ss.setActiveSheet(sheet)
// below is pdf conversion
var originalSpreadsheet = SpreadsheetApp.getActive();
var sourcesheet = originalSpreadsheet.getActiveSheet();
var sourcerange = sourcesheet.getRange('A1:B176'); // range to get - here I get all of columns which i want
var sourcevalues = sourcerange.getValues();
var data = sourcesheet.getDataRange().getValues();
var newSpreadsheet = SpreadsheetApp.create(thisSheet); // can give any name.
var sheetnow = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var projectname = SpreadsheetApp.getActiveSpreadsheet();
var sheetz = sourcesheet.copyTo(newSpreadsheet);
var destrange = sheetz.getRange('A1:B176');
destrange.setValues(sourcevalues);
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
var pdf = DriveApp.getFileById(newSpreadsheet.getId());
var theBlob = pdf.getBlob().getAs('application/pdf').setName(newname+newnamedate);
var folderID = "File_ID"; // Folder id to save in a folder.
var folder = DriveApp.getFolderById(folderID);
var newFile = folder.createFile(theBlob);
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
MailApp.sendEmail("Email","New Manager Inspection: "+newname+newnamedate, "A new Manager Inspection Report has been added to the Drive.");
ss.getSheetByName(name).getRangeList(["B176","B150:B164","B138:B146","B129:B134","B119:B125","B106:B115","B99:B102","B84:B95","B78:B80","B66:B74","B55:B62","B48:B51","B34:B44","B25:B30","B18:B21","B10:B14","B4:B7","B2","B16","B23","B32","B46","B53","B53","B64","B76","B82","B97","B104","B117","B127","B136","B148","B166"]).clearContent();
ss.setActiveSheet(sheetfinal);
SpreadsheetApp.getUi().alert("Report Submitted, Please do NOT Resubmit. Thank You");
}
I'm using =importrange() function to sync (echo sync) two sheets in two different spreadsheets (as described here). But, the importrange() it is not syncing to the second sheet when I make a change in the first sheet. Once imported, the cells stay static and do not alter as more changes are made in the first worksheet. Is there a way to fix it?
I don't think you'll be able to use the =importrange() function on two sheets because as soon as you add the function to the second sheet it will be importing the function you added to the first sheet with it's own ID as an argument.
You could use Google Apps Script, I have just answered a very similar question here. But I'll repeat what I wrote below.
One way you could accomplish this is by adding a script to both spreadsheets that copies it's contents to the other spreadsheet on a change trigger. For example if you were to add something like the below to both spreadsheets, swapping the source and destination information around.
var sourceSpreadsheetID = "ID HERE";
var sourceWorksheetName = "SHEET NAME HERE";
var destinationSpreadsheetID = "ID HERE";
var destinationWorksheetName = "SHEET NAME HERE";
function importData() {
var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
var thisData = thisWorksheet.getDataRange();
var toSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetID);
var toWorksheet = toSpreadsheet.getSheetByName(destinationWorksheetName);
var toRange = toWorksheet.getRange(1, 1, thisData.getNumRows(), thisData.getNumColumns())
toRange.setValues(thisData.getValues());
}
Just add a change trigger for the importData function and then when any changes are made to either document it will copy the contents to the other spreadsheet, thus keeping the both synced.
Obviously if both spreadsheets are being updated at the same time you will run into trouble.
That was pretty helpfull script . Have edited some changes , in your script so that even multiple sheets can be synchronized for a give column and row .the code is a bit slow but works good.
I am now thinking if there was a way to merge multiple sheets using the same method , if it does it should be awesome .
// sync multiple sheets to a source sheet ( “sheet 1”)
// change active sheet name to the designated sheet names.
function importData(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var activeSheetName = ss.getActiveSheet().getSheetName();
// set the sheet to copy from sheet 1 to sheet 2. sheet 1 active sheet.
if( activeSheetName == "Daily report Counselling" )
{
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var thisWorksheet = thisSpreadsheet.getSheetByName("Sheet1");
var thisData = thisWorksheet.getRange("A5:H");
var toSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
var toWorksheet = toSpreadsheet.getSheetByName("Sheet2");
var toRange = toWorksheet.getRange("A7:H");
toRange.setValues(thisData.getValues());
}
// if sheet 1 has not the active sheet choose from sheet 2.
if( activeSheetName == "Follow Up Needed Editable" )
{
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var thisWorksheet = thisSpreadsheet.getSheetByName("Sheet2");
var thisData = thisWorksheet.getRange("A7:H");
var toSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var toWorksheet = toSpreadsheet.getSheetByName("Sheet1");
var toRange = toWorksheet.getRange("A5:H");
toRange.setValues(thisData.getValues());
}
}
Please let me know if something interesting comes in the way .