app-script/Google Docs to Spreadsheet and vice-versa - google-apps-script

Hello :) I wonder if there is a script/Google script that takes several Google Docs Documents with the same table inside,same structure(I mean table with first name,last name,age,classroom,final grade) and puts everything in a Google Spreadsheet table so that each row identify a student to have a collection of all the students in my year and if I modify a Google Document of a student it will modify in the spreadsheet and vice-versa if I modify the data in the speadsheet(like the classroom) it will modify in the Google Doc ? Thank you very much and I know that there is autoCrat but for each student I have a Google Doc file and I would be perfect to have a spreadsheet table.
Edit : I a very sorry if my question doesn't repect your policy I've tried this code :
var doc=DocumentApp.getActiveDocument();
var body=doc.getBody();
if(body)
{
var numChildren=body.getNumChildren();
for(var i=0;i<numChildren;i++)
{
var child=body.getChild(i);
if(child.getType()==DocumentApp.ElementType.TABLE)
{
var numrows = child.asTable().getNumRows();
for(var j=0;j<numrows;j++)
{
var numcells=child.asTable().getRow(j).getNumCells();
for(var k=0;k<numcells;k++)
{
var celltxt=child.asTable().getCell(j, k).editAsText().getText();
but to export after that in a spreadsheet I am blocked :( if you have an idea

There are 2 roundabouts for this that may work for you:
-If you want a better setup from the start you can do an integration of Google Forms, Google Sites, and Google Sheets.
-If you want something that works with the info you have now, you can create a pivot sheet that pulls data via the function IMPORTHTML https://support.google.com/docs/answer/3093339?hl=en
There are other functions that may be of use: https://support.google.com/docs/table/25273?hl=en&ref_topic=9199554
Ideally if you want to set a whole system, you can directly have a Google Form fed the information into a concrete Google Sheets (and you can leave open the form's option to edit answers) and you can make multiple forms fed the same sheet that create different tabs for each form, and then have a table that combines them all.
Since you can insert Docs, Sheets and Forms into a Google Sites then you can re-plan all this from the start.
Otherwise you can just try the import functions in a pivot sheet.

Related

Google Apps Script - Creating and Saving Filters for Google Sheets

I feel like a bit of a chump, but I cannot work this out...
I have been given the job of producing a new master analysis sheet each month from a supplied XML file that combines with various columns of our (multiple) sheets. No problems, so far. I have got all of that working the way I want. :-)
My issue is that we also have about 6-8 filters saved with a specific sheet that allow our auditors to focus on specific areas (and as you can understand, our auditors want these to work EXACTLY as they specify).
I have tried using createFilter() but there doesn't appear any way to save multiple filters to that sheet (maybe I am missing something). No joy! :-(
I have tried recording a macro which I could then run to create the filters. No joy here either :-(
Do I have to tell these pesky auditors to create there own filters each month (they do know how, but it's beneath them), or is there a way I can script them up and get them off my back?
Unfortunately (as much as I would like to) I cannot share our sheets or scripts as we have significant IP embedded there.
I would really appreciate some guidance as to how you might approach this (if it is possible).
Kind regards
Ian
If you're indeed talking about the 'Create new filter view', I suggest making an template sheet. So instead of creating a new sheet every month, make one template spreadsheet and add all the filter views your auditors desire. Then copy that spreadsheet, and paste the new data in it.
The correct way to create a filter using Apps Script and the createFilter() is this one:
function setFilters() {
var ss = SpreadsheetApp.getActiveSheet();
var rangeFilter = ss.getRange("INPUT_YOUR_RANGE_HERE");
var filter = rangeFilter1.createFilter();
var filterCriteria = SpreadsheetApp.newFilterCriteria();
filterCriteria.ADD_YOUR_CRITERIA_HERE;
filter.setColumnFilterCriteria(columnPosition, filterCriteria.build());
}
As you can see, you must use build() in order to build the criteria for the filter you have created.
You can also use the Sheets advanced services and create the filters using the Sheets API, something similar to this:
var filterSettings = {
//YOUR FILTER SETTINGS
};
var request = [{
"setBasicFilter": {
"filter": filterSettings
}
}];
And as for calling the Sheets service and applying the above filter, you can use this:
Sheets.Spreadsheets.batchUpdate({'requests': request}, SPREADSHEET_ID);
Reference
Range Class Apps Script createFilter();
Filter Class Apps Script;
Apps Script Google Advanced Services.

Google Sheets - Replace sheet without breaking references to that sheet

We are building a google sheets database where each user has their own spreadsheet that accesses a central sheet for information using apps script.
This means that with 50 employees, we have 50 spreadsheets to maintain. I am trying to find a way to push updates to all 50 spreadsheets without having to update each one manually. I have all the apps script code in a library that each user's sheet references, so I have the coding maintenance figured out. But keeping each users actual spreadsheet up to date with the latest features is proving difficult.
One way I'm figuring to do that is have a "Template" user sheet that gets updated with the changes/new features. Then when each user opens their spreadsheet, it cross references all of its sheets to the template sheet, and checks if it needs to replace it's sheet with the latest sheet based on time that it was updated in the template sheet. For example, when the sheet "Project Report" in the template is newer than the "Project Report" sheet in the user's spreadsheet, the user SS deletes it's current "Project Report" and copies the template "Project Report" sheet to it's own via the copyTo() method.
I have this all working with apps script, but the issue now is that when the user's local sheet is deleted and replaced with the new updated seet, all formula references to that sheet in other sheets break and replace the reference with #REF. I had planned on overcoming this by using only Named Ranges, but even the named ranges break when the sheet is replaced to the point where even the apps script can no longer find the named range because the named range it is looking for was automatically renamed when the new version of the sheet was imported (aka, "CustomNamedRange" in the template SS was renamed to "'SheetName'!CustomNamedRange" in the user SS).
The only way I know to overcome this issue at this point is to create a centralized "Range Index" spreadsheet that has all the named ranges with their destination sheet and range. I would have to create a custom function that filters through the range index and finds the address it needs based on the name given. For example, instead of calling "CustomNamedRange" in a sheet formula, I would call custom function: getNamedRange("CustomNamedRange"), and apps script would return the range found in the range index. And when a sheet is replaced with the newer version, no references would break because all references go through the apps script filter function.
The only problem with this is that I can foresee this method (calling every range needed in the script through a custom function) slowing down my spreadsheet A LOT because every time a range is called for, it will have to go search through the range index to find it and return it.
Does anyone have any other ideas on how to accomplish what I'm looking for? As in keeping 50+ individual spreadsheets updated with new features without having to do it manually and without breaking all the references?
Sorry for the long post, but I appreciate any ideas!
I had a similar problem and was able to resolve it by using SheetAPI to replace text. I have a template called Sheet1_Template and its hidden. I delete Sheet1, copy Sheet1_Template, show it and then replace all occurances of "Sheet1" in formulas to "Sheet1". Sheet API has to be enabled in the Resources and Google API Console.
function copyTemplate() {
try {
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("Sheet1");
if( sheet !== null ) spread.deleteSheet(sheet);
sheet = spread.getSheetByName("Sheet1_Template");
sheet = sheet.copyTo(spread);
sheet.setName("Sheet1");
sheet.showSheet();
sheet.activate();
spread.moveActiveSheet(0);
var requests = {"requests":[{"findReplace":{"allSheets":true,"find":"Sheet1","replacement":"Sheet1","includeFormulas":true}}]};
Sheets.Spreadsheets.batchUpdate(requests, spread.getId());
}
catch(err) {
Logger.log("error in copyTemplate: "+err);
}
}
I haven't been able to test implementation of it yet, but I believe the answer above is what I was originally looking for.
I haven't spent any time messing with the API yet, so in the meantime I have found another solution:
Google Sheets recently added macros to it's feature set. The beauty of this is that You can see and edit the macro code after you've recorded your actions in the sheet. For now, I plan on recording a macro when I make updates to the template sheet, then copying the script for that macro into a custom function in my library that will run every time a user opens their spreadsheet. When they open their SS, apps script will check to see if the library's macro function has a later date than the last time the sheet was opened. If it does have a new date, then it will run the macro script, and that user's SS should get updated to the same state as the template.
Also if you are seeing that you cannot run the query from #TheWizEd
It may be due to "Sheets API" not being enabled at Advanced Google services. Please enable>
In the script editor, select Resources > Advanced Google services In the dialog that appears, click the on/off switch for Google Sheets API v4. Please turn on. Click OK button.
Thank you so much to TheWizEd for getting me started (please vote for that post too).
This is what I needed:
function replaceFormulasInSheet(sheet, searchFor, replaceWith) {
// https://stackoverflow.com/a/67151030/470749
// First you need to do this to enable the feature: https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services
// https://developers.google.com/sheets/api/quickstart/apps-script
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#findreplacerequest
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate
const spread = SpreadsheetApp.getActiveSpreadsheet();
const requests = {
"requests": [
{
"findReplace": {
// "allSheets": true, Omitting this property and instead setting the sheetId property is the only way to effectively set allSheets as false.
"sheetId": sheet.getSheetId(),
"find": searchFor,
"replacement": replaceWith,
"includeFormulas": true
}
}
]
};
return Sheets.Spreadsheets.batchUpdate(requests, spread.getId());
}
Also note that it does not work for sheets with hyphens in their names. If you need hyphens in their names, remove the hyphens beforehand and re-add them after.

Google Script to import data from Google trends

just wondering if there is a script to import data from google trends into a google sheet.
Basically, I would like to trigger such script daily to know the rising trends for a given topic and I am not sure if there is such solution available.
Else, is there any solution to perform this task?
Many thanks!
You may refer with this sample code snippet on how to use Google Apps Script for querying Google trends. For example, this function read input from the spreadsheet and sanitize it, then call up queryGoogleTrends to perform actual query and finally display the actual result:
function startQuery() {
sheet = SpreadsheetApp.getActiveSpreadsheet();
// start the query
var result = buildQueryString(sheet.getRangeByName("q").getValue(),
sheet.getRangeByName("cat").getValue(),
sheet.getRangeByName("geo").getValue(),
sheet.getRangeByName("gprop").getValue(),
sheet.getRangeByName("cmpt").getValue(),
sheet.getRangeByName("date").getValue()
);
// display the resulting link in a cell
sheet.getRangeByName("Query_Result").setValue(result).setBackground("yellow");
var csv_result = generateCsvDownload(sheet.getRangeByName("q").getValue(),
sheet.getRangeByName("cat").getValue(),
sheet.getRangeByName("geo").getValue(),
sheet.getRangeByName("gprop").getValue(),
sheet.getRangeByName("cmpt").getValue(),
sheet.getRangeByName("date").getValue()
);
sheet.getRangeByName("CSV_Download_Link").setValue(csv_result).setBackground("yellow");
}

Unable to access (an save) my SpreadSheet Google App Script

I have a Google SpreadSheet (filled through a form) associated to a Google Apps Script.
Now, the spreadsheet contains too many cells and I'm not able to load it anymore (server timeout).
I'm not interested in the data, but I really want to retrieve the script (the source code).
Unfortunately, the only way I know is to load the SpreadSheet first :-(
(Copying the Document ends in a timeout and the script is not copied.)
Any suggestion?
Regards,
RĂ©mi.
Create another script which makes a copy of original spreadsheet to backup the data and then clear the original spreadsheet through the script.
However this seems an issue which should be reported to Google Spreadsheet Forum
Here is an example code which will backup the spreadsheet as a copy and clear the original spreadsheet
function backupDataAndClear(){
var sourceSS = SpreadsheetApp.openById('ID_OF_ORIGINAL_SS');
var newSS = sourceSS.copy('Backup SS');
var sourceSheets = sourceSS.getSheets();
for(var i in sourceSheets){
sourceSheets[i].clear();
}
}

How to access data on different Google Spreadsheet through Google Apps Script?

I'm writing a Google Apps Script on my Google Site and am trying to use data that is provides on 2 different tabs in a Google Spreadsheet. From what I thought I understood from the documentation I could use all available methods in the SpreadsheetApp class on a Sites script by just using the openById() method.
Anyway here is what I tried to do
function doGet(e) {
var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE).getActiveSheet();
SpreadsheetApp.setActiveSheet(doc.getSheetId()[1]);
//....
}
I get the error
Cannot find method setActiveSheet(. (line 4)
I'm pulling my work off this link: Storing Data in Google Spreadsheets and also the Ui Service section listed under Building User Interfaces.
Anybody seeing what I'm doing wrong in these two lines?
setActiveSheet should be used only with the spreadsheet displayed by the UI, a sheet in a spreadsheet you have opened in your browser.
With SpreadsheetApp.openById you are opening a spreadsheet to access its data, but it doesn't open in your browser. It hasn't an UI.
I found this comments in https://developers.google.com/apps-script/class_spreadsheetapp?hl=es-ES#openById :
// The code below opens a spreadsheet using it's ID and gets the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Some examples assume your script is running into your spreadsheet. It's not your case, because you are running a script as a service, which should have its own User Interface.
I think #megabyte1024 addresses the syntax errors, but in answer to your comment to #YoArgentina:
Do you happen to know of a way to access data on different tabs then
through a service not running inside the Spreadsheet?
Does this sort of help?
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheet1A1 = sheets[0].getRange('A1').getValue();
var sheet2A1 = sheets[1].getRange('A1').getValue();
You need to access each sheet separately.
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheet = ss.getSheets()[0]; // "access data on different tabs"
ss.setActiveSheet(sheet);
There is at least one problem in these two lines. The 1st one is that the setActiveSheet method parameters is a Sheet class object and the getSheetId method returns an integer value. By the way this method (getSheetId) is not documented. The 2nd problem can happen if the SpreadsheetApp has no active spreadsheet. In this case there is the "Please select an active spreadsheet first." error. Use the SpreadsheetApp.setActiveSpreadsheet method to set an active spreadsheet.