I know there has been a lot said on the topic of a Master Sheet already. However as I haven't found the relevant answer to my question, I was hoping you could be so kind and help. The issue is very trivial I have a script that looks into the specific folder and grabs the data from those sheets. I just don't know how to modify it so that it doesn't retrieve the empty cells too ( due to the import range function sitting in the subfiles). So in the nutshell, it would be great if the code will only retrieve a range of data, for column "A" <> ""
function myFunction() {
var folder = DriveApp.getFolderById("xcv");
var filesInterator = folder.getFiles();
var file;
var fileType;
var ssID;
var combinedData = [];
var data;
while(filesInterator.hasNext()){
file = filesInterator.next();
fileType = file.getMimeType();
if(fileType === "application/vnd.google-apps.spreadsheet"){
ssID = file.getId();
data = getDataFromSpreadsheet(ssID);
combinedData = combinedData.concat(data);
} // if ends here
} // while loops ends here
Logger.log(combinedData.length);
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master");
ws.getRange("A2:BK").clearContent();
ws.getRange(2,1,combinedData.length,combinedData[0].length).setValues(combinedData);
}
function getDataFromSpreadsheet(ssID) {
var ss = SpreadsheetApp.openById(ssID);
var ws = ss.getSheetByName("ABC");
var data = ws.getRange("A2:KB" + ws.getLastRow()).getValues();
return data;
}
Try this:
function getDataFromSpreadsheet(ssID) {
var ss = SpreadsheetApp.openById(ssID);
var ws = ss.getSheetByName("ABC");
var data = ws.getRange("A2:KB" + ws.getLastRow()).getValues().filter(r => r[0] != ''});
return data;
}
Array.filter()
Related
Please help me:
I have a sheet file with 2 columns
Column A is the keyword, column B is the link to insert the keyword. Eg:
Column A Column B
Key1 Link1
Key2 Link2
... ...
How to automatically find keywords in the DOCS file and then insert Link?
Here is my idea, but it doesn't work
function insertLink() {
var file,files,folder,folders,newestFileID;
var filethaythe = DriveApp.getFilesByName('Set Link');
var ss = SpreadsheetApp.open(filethaythe.next());//ID sheet thư viện thay thế
SpreadsheetApp.setActiveSpreadsheet(ss);
SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Trang tính1');
var sheet = ss.getSheetByName('Trang tính1');
var values = sheet.getDataRange().getValues();
folders = DriveApp.getFoldersByName('test3');
while (folders.hasNext()) {
folder = folders.next();
files = folder.getFilesByType("application/vnd.google-apps.document");
while (files.hasNext()){
file = files.next();
var newestFileID = file.getId();
Utilities.sleep(500);
var currentDoc = DocumentApp.openById(newestFileID);
var dongcuoi= sh.getLastRow();
var dc = dongcuoi +1;
var rgtxt = currentDoc.getBody();
var rgrep = ss.getSheets()[0].getRange("A1:B"+dc);
var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
repA.forEach(e => rgtxt.setLinkUrl(...e));
currentDoc.saveAndClose();
break
}
};
}
I believe your goal as follows.
You want to set the hyperlink to the word on Google Document.
The words and hyperlinks are retrieved from Google Spreadsheet.
In this case, how about the following modification?
Modified script:
function insertLink() {
// 1. Retrieve values from Spreadsheet.
var file,files,folder,folders,newestFileID;
var filethaythe = DriveApp.getFilesByName('Set Link');
var ss = SpreadsheetApp.open(filethaythe.next());
var sheet = ss.getSheetByName('Trang tính1');
var values = sheet.getDataRange().getValues();
// 2. Retrieve Google Document.
folders = DriveApp.getFoldersByName('test3');
while (folders.hasNext()) {
folder = folders.next();
files = folder.getFilesByType("application/vnd.google-apps.document");
while (files.hasNext()) {
file = files.next();
var newestFileID = file.getId();
// 3. Search words and set hyperlinks on Google Document.
var currentDoc = DocumentApp.openById(newestFileID);
var rgtxt = currentDoc.getBody();
values.forEach(([a, b]) => {
var s = rgtxt.findText(a);
while (s) {
var start = s.getStartOffset();
s.getElement().asText().setLinkUrl(start, start + a.length - 1, b);
s = rgtxt.findText(a, s);
}
});
currentDoc.saveAndClose();
break
}
}
}
In order to search the word, the method of findText is used. And, the method of setLinkUrl sets the hyperlink to the searched word.
References:
findText(searchPattern, from)
setLinkUrl(startOffset, endOffsetInclusive, url)
I created a code that merges different g sheet files together into a master spreadsheet from a given folder. Now I'd like to create a filter in the code which filters column 71 (BT) for only criteria "ABC" and merges the data into one. Would you guys help a brother out?
function myFunction() {
var folder = DriveApp.getFolderById("id");
var filesInterator = folder.getFiles();
var file;
var fileType;
var ssID;
var combinedData = [];
var data;
while(filesInterator.hasNext()){
file = filesInterator.next();
fileType = file.getMimeType();
if(fileType === "application/vnd.google-apps.spreadsheet"){
ssID = file.getId();
data = getDataFromSpreadsheet(ssID);
combinedData = combinedData.concat(data);
} // if ends here
} // while loops ends here
//Logger.log(combinedData.length);
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("QwERTY");
ws.getRange("A2:BK").clearContent();
ws.getRange(2,1,combinedData.length,combinedData[0].length).setValues(combinedData);
}
function getDataFromSpreadsheet(ssID) {
var ss = SpreadsheetApp.openById(ssID);
var ws = ss.getSheetByName("XYZ");
var data = ws.getRange("A2:KB" + ws.getLastRow()).getValues().filter(r => r[0] != '');
return data;
}
You can add the following lines of code:
let filterCriteria = SpreadsheetApp
.newFilterCriteria()
.whenTextContains("ABC")
.build();
let filter = ws.getRange(1, 71).createFilter();
filter.setColumnFilterCriteria(71, filterCriteria).sort(71, true);
Assuming, that ws is your working sheet and the criteria you're looking for is when text contains "ABC", then the snippet above will create a filter on the ws sheet on the BT1 cell.
However, please bear in mind that depending on the exact criteria of your filter you can modify the current one by taking a look at the methods available for the FilterCriteriaBuilder Class here.
Reference
Apps Script FilterCriteriaBuilder Class;
Apps Script Filter Class;
Apps Script FilterCriteria Class.
Do I have script that I am trying to get a specific range (single row ie AB2:AB10) from each sheet in a folder (getfolderbyid("jkhkkkkkkk") to copy to a specific master sheet getsheetbyID("lklklkjllj")
this is what I have but it is looping through all 5000 row of the source data sheet so is very slow
///////////////////////////////////////////
function getGaccountsDatafrom() {
var folder = DriveApp.getFolderById("folderID");
var filesIterator = folder.getFiles();
*var file;
var fileType;
var ssID;
var combinedData = [];
var data;
while(filesIterator.hasNext()){
file = filesIterator.next();
fileType = file.getMimeType();
if(fileType ==="application/vnd.google-apps.spreadsheet"){
ssID = file.getId();
data = getDataFromSpreadsheet(ssID);
combinedData = combinedData.concat(data);
} //if end here
}// while ends here
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DATA");
ws.getRange("A2:AQ").clearContent();
ws.getRange(2, 1, combinedData.length, combinedData[0].length).setValues(combinedData);
}
function getDataFromSpreadsheet(ssID){
var ss=SpreadsheetApp.openById(ssID);
var ws = ss.getSheetByName("DATA");
var data = ws.getRange("CG3:DW3"+ ws.getLastRow()).getValues();
return data;
}////////////////////////////////////////////////////
Hmm... the code is not "looping through all 5000 row of the source data sheet" but iterating files in a folder and getting data from each file separately. That is what makes it run slow, and I do not know if it can be made much faster.
The getDataFromSpreadsheet() function gets too much data because of the superfluous 3 so the result will have blank rows in between. Here is a quick fix:
const data = ws.getRange('CG3:DW' + ws.getLastRow()).getValues();
I'm trying to write script for Google Spreadsheet which will upload all files linked in column A (only URLs) to specific folder (which name is written in column B).
table example
I'm partially succesfull. I can upload first linked file. But I don't know how to edit this script to upload all the files. Where am I wrong?
This script was found here and edited by me.
function downloadFile() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Pokus");
var fileURLs = sheet.getRange(1, 1, sheet.getLastRow()).getValues();
var foldernames = sheet.getRange(1, 2, sheet.getLastRow()).getValues();
for (i = 0; i < fileURLs.length; i++) {
var response = UrlFetchApp.fetch(fileURLs[i], {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob();
var folder = DriveApp.getFoldersByName(foldernames[i]).next();
if (folder != null) {
var file = folder.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
}
var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
return fileInfo;
}
}
I'm beginner in writing scripts and I'm not experienced. Could someone help?
How about this modification?
Modification points :
There is return fileInfo; in for loop. So at 1st loop, it's returned from the function. By this, only first linked file can be uploaded.
All information can be retrieved by modifying fileInfo to an array.
Modified script :
function downloadFile() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Pokus");
var fileURLs = sheet.getRange(1, 1, sheet.getLastRow()).getValues();
var foldernames = sheet.getRange(1, 2, sheet.getLastRow()).getValues();
var fileInfo = []; // Added
for (i = 0; i < fileURLs.length; i++) {
var response = UrlFetchApp.fetch(fileURLs[i], {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob();
var folder = DriveApp.getFoldersByName(foldernames[i]).next();
if (folder != null) {
var file = folder.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
}
fileInfo.push({ "rc":rc, "fileName":fileName, "fileSize":fileSize }); // Modified
}
return fileInfo; // Modified
}
Note :
When there are many upload files, an error might occur.
If I misunderstand your question, I'm sorry.
I'm stuck and can't find an answer for my problem.
Here's my current code:
var monthSheet = sourcesheet.getRange('A1').getValue();
var destsheet = shoot.getSheetByName(monthSheet);
monthSheet is String
destsheet is always null and I don't know why...
edit:
function sendAnnouncement() {
var files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
var sourceAnn = sourcesheet.getRange('B2:B2').getValue();
var sourceMonth = sourcesheet.getRange('A2').getValue();
sourcesheet.getRange('A1').setFormula("=VLOOKUP(A2;Settings!B1:C12;2;false)");
var monthSheet = sourcesheet.getRange('A1').getValue();
var sourceTaxStake = sourcesheet.getRange('C2').getValue();
var destsheet = shoot.getSheetByName(monthSheet);
var destTaxStake = destsheet.getRange('H2').getValues();
if ('sourceTaxStake' == "all")
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
else if ('destTaxStake' == 'sourceTaxStake')
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
else
{
}
}
}
Again, that part of code itself is okay. Have you checked what value is monthSheet? Probably it's different from the actual sheet name. Also check its value by Logger.log(monthSheet). Check my example source sheet and destination sheet. Below script is included in the source sheet.
function myFunction() {
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
var monthSheet = sourcesheet.getRange('A1').getValue();
var shoot = SpreadsheetApp.openById('1JxjM7KJRofTRxH5pRpY4vLuY9ojKP__DYjodSIN3rVE');
var destsheet = shoot.getSheetByName(monthSheet);
Logger.log(destsheet.getName()); // dest_s
}
Of course 1JxjM7KJRofTRxH5pRpY4vLuY9ojKP__DYjodSIN3rVE is my destination sheet. And dest_s is the value of Announcements!A1 on the source sheet and, in the same time, the name of sheet on the destination sheet. Check for yourself.
I noticed that sourceTaxStake and destTaxStake had single quotes around them so neither condition would ever by true. I removed the single quotes and the editor immediately darkened them as it does with defined variables.
function sendAnnouncement()
{
var files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx").getFiles()
while (files.hasNext())
{
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
var sourceAnn = sourcesheet.getRange('B2').getValue();
var sourceMonth = sourcesheet.getRange('A2').getValue();
sourcesheet.getRange('A1').setFormula("=VLOOKUP(A2;Settings!B1:C12;2;false)");
var monthSheet = sourcesheet.getRange('A1').getValue();
var sourceTaxStake = sourcesheet.getRange('C2').getValue();
var destsheet = shoot.getSheetByName(monthSheet);
var destTaxStake = destsheet.getRange('H2').getValues();
if (sourceTaxStake == "all")
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
else if (destTaxStake == 'sourceTaxStake')
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
}
}
Thanks for help. There was problem with correct name of the sheet.
I've also removed single quotes from conditions. Everything is working correctly.