Google Apps Script - Appending data from spreadsheet to table in document template - google-apps-script

I am trying to write a script that will take a list of expenses from a google sheet and append them to a already existing table in a google docs template. At the moment I have this (the reason it says names is because I am testing with dummy data of names and ages):
function myFunction() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet()
var numRows = sheet.getLastRow()
var numColumns = sheet.getLastColumn()
var data = sheet.getRange(1, 1, numRows, numColumns).getValues()
var doc = DocumentApp.openById('13l6O8nmEZgJiZnzumWihsRVOZiq_8vUj6PtBtb9My_0')
var body = doc.getBody()
var tables = body.getTables()
var table = tables[1]
for (var i = 1; i < data.length; i++){
var row = data[i]
var name = row[0]
var age = row[1]
var state = row[2]
var done = 'Done'
//Check to see if line has already been processed or not
if (!state){
sheet.getRange(i + 1, 3).setValue(done)
//Slices out the blank state value
var slice = row.slice(0, numColumns-1)
table.appendTableRow(slice)
}
}
}
This just adds a new table to the document but I can't find way to add rows to an existing table of the data I can add indvidual cells one per row but that isn't useful and can't seem to/don't understand how the appendtoRow instruction works. Any thoughts on how best to do this?

To add a row to an existing table you need to use appendTableRow() and then add cells to this newly added row.
Change the last part of your script like below (make sure that your table is indeed the second table in the document since you used var table = tables[1])
...
if (!state){
sheet.getRange(i + 1, 3).setValue(done);
//Slices out the blank state value
var slice = row.slice(0, numColumns-1);
Logger.log(slice);
var tableRow = table.appendTableRow();
for(var n in slice){
tableRow.appendTableCell(slice[n]);
}
}
...

Related

Getting Specific Rows from Another Google Sheet via App Script Based on Multiple Cell Values

I would like to fetch data from another google sheet, then retrieve all columns(been successful with this one). As for the rows, I would only like to retrieve all rows with these values, "Mrm1, Mrm2, Mrm3" from Measurement Column.
For example, I have these on another sheet,Original Data
And on another sheet, I want the result to be like this: Expected Retrieved Data
So far, I have this, but I'm not sure how to apply what I need on the rows. This script is retrieving everything.
function Country A() {
var sss = SpreadsheetApp.openById("19-idD8PWuNxnvzRVqBlKhju2RkKxOe8nUQaf9ZMAcgk"); // Source spreadsheet - replace with actual key
var ss = sss.getSheetByName('Compiled'); // Source sheet - change to actual name
var numRows = ss.getLastRow() // get last row with data
var numColumn = ss.getLastColumn(); // get last column with data
var srange = ss.getRange(2,1,numRows,numColumn); // Source range - change to actual range
var values = srange.getDisplayValues();
var lss = SpreadsheetApp.getActiveSpreadsheet(); // Local spreadsheet - the one this script is in
var targetnumRows = lss.getSheetByName('Country A').getMaxRows();
var targetnumcolumns = lss.getSheetByName('Country A').getMaxColumns();
var ls = lss.getSheetByName('Country A').getRange(2,1,numRows,numColumn); // Local sheet - change to actual name and Local range - change to actual range
var TargetRange = lss.getSheetByName('Country A').getRange(2,1,targetnumRows,targetnumcolumns); // Local sheet - change to actual name and Local range - change to actual range
TargetRange.clear() //Clear Sheet Content
ls.setValues(values); // Copy from source sheet to local sheet
var sheet = lss.getSheetByName('Country A');
var maxColumns = sheet.getMaxColumns();
var lastColumn = sheet.getLastColumn();
var maxRows = sheet.getMaxRows();
var lastRow = sheet.getLastRow();
if (maxColumns-lastColumn != 0){sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn)};
if (maxRows-lastRow != 0){sheet.deleteRows(lastRow+1, maxRows-lastRow)};
var TargetHour = lss.getSheetByName('Country A').getRange(1,1).setValue(Utilities.formatDate(new Date(), "CST", "MM-dd-yyyy HH:mm:ss"));
}
Please help me. Thank you.
The script stores the values in a 2D array. You have to iterate through the elements and delete the elements where column E (Col number 4) (A=0, B=1,...E=4) is not equal to "Mrm1, Mrm2, Mrm3".
Then you paste the modified array in the second sheet.
Here is an example. You need to adapt it to your code.
function copyData() {
var ss = SpreadsheetApp.openById("YOUR-ID");
// Stores data as a 2D Array
var data = ss.getSheetByName("Sheet1").getRange("A1:M10").getValues();
// Remove rows where column E not equals "Mrm1, Mrm2, Mrm3"
for (var i = 0; i < data.length; i++) {
if (data[i][4] !== "Mrm1" && data[i][4] !== "Mrm2" && data[i][4] !== "Mrm3") {
data.splice(i,1);
i--;
}
}
// Paste data in sheet 2
var pasteRange = "A1:M" + (data.length);
ss.getSheetByName("Sheet2").getRange(pasteRange).setValues(data);
}

I need to split a Google Sheet into multiple tabs (sheets) based on column value

I have searched many possible answers but cannot seem to find one that works. I have a Google Sheet with about 1600 rows that I need to split into about 70 different tabs (with about 20-30 rows in each one) based on the value in the column titled “room”. I have been sorting and then cutting and pasting but for 70+ tabs this is very tedious.
I can use the Query function but I still need to create a new tab, paste the function and update the parameter for that particular tab.
This script seemed pretty close:
ss = SpreadsheetApp.getActiveSpreadsheet();
itemName = 0;
itemDescription = 1;
image = 2;
purchasedBy = 3;
cost = 4;
room = 5;
isSharing = 6;
masterSheetName = "Master";
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Update Purchases')
.addItem('Add All Rows To Sheets', 'addAllRowsToSheets')
.addItem('Add Current Row To Sheet', 'addRowToNewSheet')
.addToUi();
}
function addRowToNewSheet() {
var s = ss.getActiveSheet();
var cell = s.getActiveCell();
var rowId = cell.getRow();
var range = s.getRange(rowId, 1, 1, s.getLastColumn());
var values = range.getValues()[0];
var roomName = values[room];
appendDataToSheet(s, rowId, values, roomName);
}
function addAllRowsToSheets(){
var s = ss.getActiveSheet();
var dataValues = s.getRange(2, 1, s.getLastRow()-1, s.getLastColumn()).getValues();
for(var i = 0; i < dataValues.length; i++){
var values = dataValues[i];
var rowId = 2 + i;
var roomName = values[room];
try{
appendDataToSheet(s, rowId, values, roomName);
}catch(err){};
}
}
function appendDataToSheet(s, rowId, data, roomName){
if(s.getName() != masterSheetName){
throw new Error("Can only add rows from 'Master' sheet - make sure sheet name is 'Master'");
}
var sheetNames = [sheet.getName() for each(sheet in ss.getSheets())];
var roomSheet;
if(sheetNames.indexOf(roomName) > -1){
roomSheet = ss.getSheetByName(roomName);
var rowIdValues = roomSheet.getRange(2, 1, roomSheet.getLastRow()-1, 1).getValues();
for(var i = 0; i < rowIdValues.length; i++){
if(rowIdValues[i] == rowId){
throw new Error( data[itemName] + " from row " + rowId + " already exists in sheet " + roomName + ".");
return;
}
}
}else{
roomSheet = ss.insertSheet(roomName);
var numCols = s.getLastColumn();
roomSheet.getRange(1, 1).setValue("Row Id");
s.getRange(1, 1, 1, numCols).copyValuesToRange(roomSheet, 2, numCols+1, 1, 1);
}
var rowIdArray = [rowId];
var updatedArray = rowIdArray.concat(data);
roomSheet.appendRow(updatedArray);
}
But I always get an unexpected token error on line 51 or 52:
var sheetNames = [sheet.getName() for each(sheet in ss.getSheets())];
(And obviously the column names, etc. are not necessarily correct for my data, I tried changing them to match what I needed. Not sure if that was part of the issue.)
Here is a sample of my data: https://docs.google.com/spreadsheets/d/1kpD88_wEA5YFh5DMMkubsTnFHeNxRQL-njd9Mv-C_lc/edit?usp=sharing
This should return two separate tabs/sheets based on room .
I am obviously not a programmer and do not know Visual Basic or Java or anything. I just know how to google and copy things....amazingly I often get it to work.
Let me know what else you need if you can help.
Try the below code:
'splitSheetIntoTabs' will split your master sheet in to separate sheets of 30 rows each. It will copy only the content not the background colors etc.
'deleteTabsOtherThanMaster' will revert the change done by 'splitSheetIntoTabs'. This function will help to revert the changes done by splitSheetIntoTabs.
function splitSheetIntoTabs() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
var header = rows[0];
var contents = rows.slice(1);
var totalRowsPerSheet = 30; // This value will change no of rows per sheet
//below we are chunking the toltal row we have into 30 rows each
var contentRowsPerSheet = contents.map( function(e,i){
return i%totalRowsPerSheet===0 ? contents.slice(i,i+totalRowsPerSheet) : null;
}).filter(function(e){ return e; });
contentRowsPerSheet.forEach(function(e){
//crate new sheet here
var currSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
//append the header
currSheet.appendRow(header);
//populate the rows
e.forEach(function(val){
currSheet.appendRow(val);
});
});
}
// use this function revert the sheets create by splitSheetIntoTabs()
function deleteTabsOtherThanMaster() {
var sheetNotToDelete ='Master';
var ss = SpreadsheetApp.getActive();
ss.getSheets().forEach(function(sheet){
if(sheet.getSheetName()!== sheetNotToDelete)
{
ss.deleteSheet(sheet);
}
});
}
I was using Kessy's nice script, but started having trouble when the data became very large, where the script timed out. I started looking for ways to reduce the amount of times the script read/wrote to the spreadsheet (rather than read/write one row at a time) and found this post https://stackoverflow.com/a/42633934
Using this principle and changing the loop in the script to have a loop within the loop helped reduce these calls. This means you can also avoid the second call to append rows (the "else"). My script is a little different to the examples, but basically ends something like:
`for (var i = 1; i < theEmails.length; i++) {
//Ignore blank Emails and sheets created
if (theEmails[i][0] !== "" && !completedSheets.includes(theEmails[i][0])) {
//Set the Sheet name = email address. Index the sheets so they appear last.
var currentSheet = theWorkbook.insertSheet(theEmails[i][0],4+i);
//append the header
//To avoid pasting formulas, we have to paste contents
headerFormat.copyTo(currentSheet.getRange(1,1),{contentsOnly:true});
//Now here find all the rows containing the same email address and append them
var theNewRows =[];
var b=0;
for(var j = 1; j < rows.length; j++)
{
if(rows[j][0] == theEmails[i][0]) {
theNewRows[b]=[];//Initial new array
theNewRows[b].push(rows[j][0],rows[j][1],rows[j][2],rows[j][3],rows[j][4],rows[j][5],rows[j][6],rows[j][7]);
b++;
}
}var outrng = currentSheet.getRange(2,1,theNewRows.length,8); //Make the output range the same size as the output array
outrng.setValues(theNewRows);
I found a table of ~1000 rows timed out, but with the new script took 6.5 secs. It might not be very neat, as I only dabble in script, but perhaps it helps.
I have done this script that successfully gets each room and creates a new sheet with the corresponding room name and adding all the rows with the same room.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
// This var will contain all the values from column C -> Room
var columnRoom = sheet.getRange("C:C").getValues();
// This var will contain all the rows
var rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
//Set the first row as the header
var header = rows[0];
//Store the rooms already created
var completedRooms = []
//The last created room
var last = columnRoom[1][0]
for (var i = 1; i < columnRoom.length; i++) {
//Check if the room is already done, if not go in and create the sheet
if(!completedRooms.includes(columnRoom[i][0])) {
//Set the Sheet name = room (except if there is no name, then = No Room)
if (columnRoom[i][0] === "") {
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet("No Room");
} else {
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(columnRoom[i][0]);
}
//append the header
currentSheet.appendRow(header);
currentSheet.appendRow(rows[i]);
completedRooms.push(columnRoom[i][0])
last = columnRoom[i][0]
} else if (last == columnRoom[i][0]) {
// If the room's sheet is created append the row to the sheet
var currentSheet = SpreadsheetApp.getActiveSpreadsheet()
currentSheet.appendRow(rows[i]);
}
}
}
Please test it and don't hesitate to comment for improvements.

Javascript on Google Sheets - Get Column Number from Prompt Input

I have two sheets on a Google Spreadsheet. One has a lot of information and references and the other has the same reference in the first cell of a column with link names and links below. I am trying to get around the "no multi-hyperlinking in one cell" limitation by having the user input the reference they want to search and then searching through the second sheet to find the reference and have a pop-up box with the links.
So far, I am able to get the links from the second spreadsheet column and display them in a pop-up box with this code:
function main(){
var column = SearchAndFind()
showURL(getLinks(column))
}
function getLinks(col){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.setActiveSheet(ss.getSheets()[1]);
var cell = ss.getActiveCell();
var values = sh.getDataRange().getValues();
var myArray = []
for(n=1;n<values.length;++n){
var cell = values[n][col] ;
myArray.push(cell);
}
return myArray;
}
function showURL(data){
var app = UiApp.createApplication().setHeight(40+8*data.length).setWidth(200);
app.setTitle("Show URLs");
var panel = app.createVerticalPanel();
app.add(panel);
for(var d=0 ; d<data.length;d=d+2){
var link = app.createAnchor(data[d],data[(d+1)]);
panel.add(link);
}
var doc = SpreadsheetApp.getActiveSpreadsheet();
doc.show(app);
return;
}
When I hard-coded a random column number to the getLinks function and it worked fine but I need to be able to get the column number from a user search of the first cell in each column in the second sheet.
This is the code I have right now that doesn't work:
//I know that it will always be the second sheet on the spreadsheet
//Search the column headers on the second sheet
//When one matches, return the index
function SearchAndFind(){
//Make the 2nd sheet active
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.setActiveSheet(ss.getSheets()[1]);
var range = sh.getRange(1, 1, sh.getMaxRows(), 1);
var values = range.getValues();
//Get the user input for the text they want to search
var ui = SpreadsheetApp.getUi();
var search = ui.prompt('Enter the ID: ');
var searchString = search.getResponseText()
//for loop to iterate through the first row and find the matching cell
//return the index of that column
for (n = 0; n < values[0].length; n++){
var cell = values[0][n]
if (cell === searchString){
return n;
}
}
}
When I run all of the code (including the function SearchAndFind that doesn't work), the pop-up box comes up with undefined, linking nowhere. Admittedly, I don't have a lot of experience with Javascript so I think I just don't understand it well enough to find the bug here.
You are pulling only one column and then checking for the match in columns.
var range = sh.getRange(1, 1, sh.getMaxRows(), 1);
Gives you only the first column. And
for (n = 0; n < values[0].length; n++){
Looks through those columns. So your values[0].length is 1, and your loop only runs once.
Are you trying to loop through all rows form first column, or through first row of all columns.
Also you should change your loop to
for (var n = 0; n < values[0].length; n++){

Google Script to copy rows to another spread and allow for editing of copy data

I have a form that populates a master spreadsheet, and I have another spreadsheet that gets data from the master sheet. Using the script below I can't edit the information that is copied because whenever the script runs it replaces the edited information to match the master sheet. How can I get the script to only copy data if data hasn't been entered.
ColumnQ's data never changes.
function myFunction() {
var source = SpreadsheetApp.openById('XXX');
var sourcesheet = source.getSheetByName('Lead Sheet');
var target = SpreadsheetApp.openById('XXX')
var targetsheet = target.getSheetByName('Lead Sheet');
var targetrange = targetsheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
targetrange.setValues(rangeValues);
}
Do you mean you only want to copy over new rows from the "master" sheet that aren't already in the "target" sheet? If so, assuming column Q is like your "unique key," then you'll have to pull at least this column from each sheet and compare the results. Here's some tested code that does what I believe you want:
function myFunction() {
var qOffset = 16;
var ss = SpreadsheetApp.openById('');
var sourceSheet = ss.getSheetByName('Source');
var targetSheet = ss.getSheetByName('Target');
var sourceValues = sourceSheet.getDataRange().getValues();
var targetValues = targetSheet.getDataRange().getValues();
var deltaValues = [];
for(var i = 0 ; i < sourceValues.length ; i++){
var rowsMatch = false;
for(var j = 0 ; j < targetValues.length ; j++){
if(sourceValues[i][qOffset] == targetValues[j][qOffset]){
rowsMatch = true;
break;
}
}
if(!rowsMatch){
deltaValues.push(sourceValues[i]);
}
}
if(deltaValues.length > 0){
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, deltaValues.length, deltaValues[0].length).setValues(deltaValues);
}
}
Also, I just recently tackled a similar project, but instead of using a "master" sheet, I wrote a function that's triggered by form submissions. When the function is called, it searches my sole spreadsheet for the data; if it finds it, it updates the row, if not, it appends a new row. If that's of any interest to you, I can post parts of the source code.

Unable to process the operation because it contains too much data. Google Apps Script

I have two sheets.
First is data from JSON with max results 100 rows. It contains 8 columns.
Second is the data I add manually and then write to the first sheet based on matched title.
For example, if both titles match then create a new column "Category" in first sheet from second sheet. The second sheet contains 50 rows and 8 columns.
When I run this script it throws error: We're sorry, we were unable to process the operation because it contains too much data. I tried to remove line by line to figure out what is causing it. And it seems like when I remove this line:
data[i][11] = descr; // this is a paragraph long description text
It is working fine. Also, if I remove all the other data I want to write in, and run only data[i][11] = descr; it also chokes. So, it doesn' seem like there is too much data. Any ideas how to make it work? Workarounds?
Edit: here is a copy of the spreadsheet:
https://docs.google.com/spreadsheet/ccc?key=0AhVWrVLsk5a5dFRaeFQxZUc3WlZOR0h4N09pOGJBdGc&usp=sharing
Thanks!
function listClasses(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; //list of all upcoming classes
var sheet1 = ss.getSheets()[1]; //list of titles
var data = sheet.getDataRange().getValues(); // read all data in the sheet
var data1 = sheet1.getDataRange().getValues();
for(n=1;n<data1.length;n++){
var title1 = data1[n][0];
var category = data1[n][1];
var image_url = data1[n][2];
var available = data1[n][3];
var descr = data1[n][4];
var prerequisites = data1[n][5];
var mAccess = data1[n][6];
var notes = data1[n][7];
// compare Check if title appears in column B of sheet 1.
if (ArrayLib.find(data1, 0, title1) != -1) {
// Yes it does, do something in sheet 0
for( var i = data.length -1; i >= 0; --i )
if (data[i][1] == title1)
{
//Logger.log(descr);
if (data[i].length < 14){
data[i].push(' ');
}
data[i][8] = category;
data[i][9] = image_url;
data[i][10] = available;
data[i][11] = descr;
data[i][12] = prerequisites;
data[i][13] = mAccess;
data[i][14] = notes;
sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}
}
}
I had a look at your code, but I could not make it work.
I too get such error messages, but so far have encountered them when I am processing a few thousand rows (with only 4 columns) but each row containing as much text or more than your description.
I only know how to use a simple way, which I tried for your case too. The following code I think does what you need:
function listClasses2(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; //list of all upcoming classes
var sheet1 = ss.getSheets()[1]; //list of titles
var data = sheet.getDataRange().getValues(); // read all data in the sheet
var data1 = sheet1.getDataRange().getValues();
var newDataArray = [data[0]];
for(var n=1; n < data.length ; n++){
for(var j=0; j< data1.length ; j++){
if(data[n][1] == data1[j][0]){
newDataArray.push([data[n][0] ,data[n][1] ,data[n][2] ,data[n][3] ,data[n][4] ,data[n][5] ,data[n][6] ,data[n][7] ,data1[j][1] , data1[j][2] , data1[j][3] , data1[j][4] , data1[j][5] , data1[j][6], data1[j][7] ]) ;
break;}
}
newDataArray.push(data[n])
}
sheet.getRange(1, 1, newDataArray.length, newDataArray[0].length).setValues(newDataArray);
}
(I have just noticed that my var n is not the same as your var n ... sorry for the possible confusion)