I have a scrip that is adding in HTM into a cell that it copies from a text file.
The script goes to the Google Drive location, copies the html text in the txt file and then pastes it into a cell.
When I go to have the code placed in the html viewer it ads an additional " into the code. Viewing the code in the Google sheet shows it correctly. It is only when copy and pasted into the html view that the second " is found. It is also found when pasting the code into the site.
If I copy the script from going into the formula bar and selecting all than it works in the HTML viewer. It is only when copying the cell by selecting it.
Example:
Correct paste -
<td><img src="https://www.google.com/maps/vt/data=0VFu_F4Bx-5lwDGqJYDI6I6tr3Mbedx2smpbqtdC1yluR4_Fj3hhz7AXWCuAx5aYEfLwo_4E6ruoO-RpRwz5cFXCO_UGm2leq-0C_3I" width="262" height="175"><br>Sycamore Drive</td>
What happens when pasted into html viewer from copy and pasting by selecting the cell:
<td><img src=""https://www.google.com/maps/vt/data=0VFu_F4Bx-5lwDGqJYDI6I6tr3Mbedx2smpbqtdC1yluR4_Fj3hhz7AXWCuAx5aYEfLwo_4E6ruoO-RpRwz5cFXCO_UGm2leq-0C_3I"" width=""262"" height=""175""><br>Sycamore Drive</td>
This is the script to grab the text from the google folder.
function importTextFilesAsValues() {
var folder = DriveApp.getFolderById('google folder ID');
var files = folder.getFilesByType(MimeType.PLAIN_TEXT);
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
while (files.hasNext()) {
var file = files.next();
var content = file.getBlob().getDataAsString();
sheet.getRange(row, 1).setValue(content).setNumberFormat("#");
row++;
}
}
I tried copy and pasting the cell which did not work. Only when copying the text by doing it through the formula bar it works.
Related
I try to move a file from my root-folder into a defined folder. But my script send me the fautl: "Exception: The parameters (String) don't match the method signature for DriveApp.File.moveTo." - but why?
Does anyone has a tip for me?
function myMoveFile() {
var myFolderID = DriveApp.searchFolders("Title Contains 'Final_Folder'").next().getId();
var myNewDoc = DocumentApp.create("Doc per Script");
var myFileID = myNewDoc.getId();
DocumentApp.openById(myFileID).getBody().appendParagraph("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT");
Logger.log(myFolderID);
DriveApp.getFileById(myFileID).moveTo(myFolderID);
}
Thanks
destination of moveTo(destination) is the folder object which is not the folder ID. I thought that this might be the reason for your issue. So how about the following modification?
Modified script:
function myMoveFile() {
var myFolder = DriveApp.searchFolders("Title Contains 'Final_Folder'").next();
var myNewDoc = DocumentApp.create("Doc per Script");
var myFileID = myNewDoc.getId();
DocumentApp.openById(myFileID).getBody().appendParagraph("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT");
Logger.log(myFolder.getId());
DriveApp.getFileById(myFileID).moveTo(myFolder);
}
Reference:
moveTo(destination)
Added:
About the following 2nd question,
My intention is to create a file via GScript and then move it to a defined folder (e.g. "Final_Folder"). There are 2 folders with the same name that I want to move I avoid that the file is moved to the wrong folder.
In this case, as a workaround, how about using the star? At Google Drive, a star can be added to the files and folders. In your situation, when the same folders are existing and you want to move the file to one of those folders, please add a star to the specific folder you want to use. I thought that this might be a workaround for identifying the specific folder from the same folder name.
Sample script:
Before you run this script, please add a star to the folder you want to use. And, run this script. By this, the file is moved to the starred folder. By this, you can use the specific folder from the same folder names.
function myMoveFile() {
var folders = DriveApp.searchFolders("Title Contains 'Final_Folder'");
while (folders.hasNext()) {
var myFolder = folders.next();
if (myFolder.isStarred()) {
var myNewDoc = DocumentApp.create("Doc per Script");
var myFileID = myNewDoc.getId();
DocumentApp.openById(myFileID).getBody().appendParagraph("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT");
Logger.log(myFolder.getId());
DriveApp.getFileById(myFileID).moveTo(myFolder);
break;
}
}
}
I have a Google Sheets sheet that creates QR images from the Google charts API using the IMAGE function that takes a URL to encode as an argument. I would like to export the created images to an image file, say a JPG. Problem is Sheets does not recognize the cell content as an image so SHIFT + Right click does not show the Save Image As in the context menu. Any ideas on how to export all of these to separate files.
Download the sheet in Microsoft Excel .xlsx format by going to the File > Download menu.
Then open the .xlsx file in your computer using any unzip program (e.g. 7z) and navigate to ../xl/media in the zip file. All the images would be there - extract where you want them.
This will fit your needs:
Tools -> Script editor
Clear the little code you see
Paste the code from below
Change sheetname (now Data) to your needs
sheet.getRange(2,1, ..... This assumes your images starting at row 2 in the first column (A). Change this to fit your needs.
Changed the folder id to the folder you want the images. (found in url)
Hit run -> Give permission (once)
Look at the folder.
The script:
function isImageToImage(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Data');
const formulas = sheet.getRange(2,1, sheet.getLastRow()-1).getFormulas().flat();
const urls = formulas.map(formula => {
return /\"(.*?)\"/gmi.exec(formula)[1]
})
const folder = DriveApp.getFolderById('1QVo_pr_Wo6xxxxxxxxx9Yx')
urls.forEach(url => {
console.log(url)
const blob = UrlFetchApp.fetch(url).getBlob();
folder.createFile(blob)
})
}
We are using sheets for our mom & pop store as our invoice. However, my mother-in-law keeps saving over our invoice and we're always having to go back and delete the filled in sections. She cannot seem to remember the steps of making a copy and then opening that. I am using a script (button) to create a copy of the original and it is renamed as the customer name & date into a specific folder. However, once we do that, we still have to navigate to the folder and open the new document. Is there there a way to do this after I click the button on our original document to open of the copy that was made? Here is the script that I am using.
function saveAsSpreadsheet()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange('Carolina Fireworks Order Form!C8');
sheet.setNamedRange('TestRange', range);
var TestRange = sheet.getRangeByName('Carolina Fireworks Order Form!C8').getValues();
Logger.log(TestRange);
var destFolder = DriveApp.getFolderById("1UdK90fEs3gkP4KZuUbmZbvvyVAW5ZMGw");
let name = SpreadsheetApp.getActiveSheet().getRange('C8').getValue();
const ds = Utilities.formatDate(new Date(),SpreadsheetApp.getActive().getSpreadsheetTimeZone(),"MM.dd.yy");
DriveApp.getFileById(sheet.getId()).makeCopy('TestRange', destFolder).setName(`${name}.${ds}`);
}
In your situation, how about the following modification?
From:
DriveApp.getFileById(sheet.getId()).makeCopy('TestRange', destFolder).setName(`${name}.${ds}`);
To:
const file = DriveApp.getFileById(sheet.getId()).makeCopy('TestRange', destFolder).setName(`${name}.${ds}`);
var js = `<script>window.open('${file.getUrl()}');google.script.host.close();</script>`;
var html = HtmlService.createHtmlOutput(js);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
In this script, the URL is opened using Javascript on the dialog.
This is from this post
Note:
From MetaMan's comment
If you want this to work you will have to disable popup blocking
References:
showModalDialog(userInterface, title)
Window.open()
I need a way to open a spreadsheet only by having the name of the corresponding file and the folder where the file is located. What I'm trying to do is creating a temporary copy of an active spreadsheet and reading information from the copy file rather than the active one. But unfortunately this copy file won't have a constant ID and URL as it's going to be erased every time after the script is done, and re-created with the next running of the script.
What I already tried is using the URL of the temporary file in order to open the spreadsheet itself, as you can see below.
But it's not useful for me, as I don't have a constant URL of this file (as it will be erased in the end of the script and have a new URL with the next script). But I have a constant name of the file, as well as this will be the only file in the respective folder.
What I also tried is looking for methods to open file only using its name (which is "temp_copy" in a 'temp_folder'). But I didn't find a working solution.
var report = SpreadsheetApp.getActiveSpreadsheet();
var temp_folder =
DriveApp.getFolderById("1EAsvVcFjIw5iGTeF_SxxQJLVlaLHl_pR");
DriveApp.getFileById(report.getId()).makeCopy("temp_copy", temp_folder);
var temp_copy = SpreadsheetApp.openByUrl(' ');
var hiddenSheet = temp_copy.getSheetByName('DATE');
var lastRow = hiddenSheet.getSheetValues(1,1,1,1);
var sheet = temp_copy.getSheetByName('forCSV');
var lastColumn = sheet.getLastColumn();
var activeRange = sheet.getRange(1,2,lastRow,lastColumn);
What I'm expecting as a result is to make the link between the 3rd line of the code (when making a copy of the basic file) and 5th line of the code onwards (when referencing specific range from the temporary file) only by using name of the file or respective folder. In theory it seems so easy, I should be missing something somewhere...
Many thanks in advance!
You can use DriveApp to get a FileIterator which contains the Sheet.
var file, files = DriveApp.getFilesByName("temp_copy");
if (files.hasNext ()){
file = files.next(); // Get first result for name
} else {
return "";
}
var sheet = SpreadsheetApp.openById(file.getId())
I was wondering if someone could offer me some advice.
I have a master spreadsheet, acting as a template. I have written a script which can be run from the menu (using addToUi command), which makes a copy of template spreadsheet.
The problem is that the script gets copied into the new spreadsheet also which I don't want.
Could anyone suggest a possible way around this please?
I did think a possible way was to get the script to open the copied template and delete the script but not sure if this is possible.
Here is the function which does the copying....
function createCopy() {
var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("B8").getValue();
var destinationFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxx");
DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(myValue,destinationFolder);
}
(Cell reference B8 holds the value of what I called the copied spreadsheet).
Rayden, I use a function like that to just copy one sheet to a new spreadsheet and it doesn't drag the script with it. gDrive is the id for the Spreadsheet, tabName the individual sheet you want copied, Filename the name of the copy and destination the destination directory.
//******************************************************************************
//- This function takes a tab and makes it its own file
function tabToSheet(gDrive,tabName,fileName,destination){
var sh = SpreadsheetApp.openById(gDrive);
var ss = sh.getSheetByName(tabName);
//create a new document in the location given
var newSheet = SpreadsheetApp.create("TEMPDELETEME");
//copy the tab from current document to new document
ss.copyTo(newSheet);
var id = newSheet.getId();
newSheet.deleteSheet(newSheet.getSheetByName("Sheet1"));
os = newSheet.getSheets()[0];
os.setName(tabName);
var file = DriveApp.getFileById(id);
var folder = DriveApp.getFolderById(destination);
var finalId = file.makeCopy(fileName, folder).getId();
file.setTrashed(true);
return finalId;
}//*****************************************************************************
The difference is that i'm making a new sheet, then copying the tab rather than copying the entire file. You could add another tab and remove the variables if you want to copy multiple tabs.
I was having trouble implementing J.G.'s approach of moving individual sheets, so the approach I took was to add a conditional in front of the script to only run if the spreadsheet id was equal to the id of the original spreadsheet. My use case was trying to suppress a custom menu on the original workbook from reappearing on the copy, so it worked for that.
var bookId = spreadsheet.getId();
if (bookId === 'original spreadsheet id') {
*Function*
}