Linking Files in G++

Linking files in g++

You probably tried to either compile and link instead of just compiling source files or somehow forgot something.

Variation one (everything in one line; recompiles everything all the time):

g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]

Variation two (step by step; if no -o is provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):

g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]

Variation three (some placeholders):

Won't list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cpp will compile all cpp files in current directory to o(bject) files of the same name.

Overall you shouldn't worry too much about it unless you really have to work without any IDE. If you're not that proficient with the command line syntax, stick to IDEs first.

Google Drive Links & File Names

The reason why you encountered such error is because your array ids is empty.
When you use FileIterator.next() to gets the next item in the collection of files, You finished checking all the items in your file from your first while loop. Hence, the second while loop will not be executed anymore. (You can include logs to debug your code. Initially, your files.hasNext() will return false in your second while loop).

You can actually list the file name and construct your url using your first while loop.

Sample Code:

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var c1 = s.getRange("B2");
var c2 = s.getRange("A2")
var fldr = DriveApp.getFolderById("14ZKphLE02f6tdyU7LGEjbEEfrfD89nJM");
var files = fldr.getFiles();
var urls = [], ids = [], f, str;
while (files.hasNext()) {
f = files.next();
str = '=hyperlink("' + f.getUrl() + '")';
urls.push([str]);
//ids.push([f.getName()]); //Filename with extensions
ids.push([f.getName().replace(/\.[^/.]+$/, "")]); //Remove filename extensions
}

s.getRange(c1.getRow(), c1.getColumn(), urls.length).setFormulas(urls);
s.getRange(c2.getRow(), c2.getColumn(), ids.length).setValues(ids);

}

Output:

Sample Image

Using Google Apps Scripts to generate shareable links of new files

Yes almost everything is possible in GAS. However, you are likely to find better answers when you post some code that you have tried yourself before asking the answer here.

However, if the machine automatically saves the files in a folder in google drive you can scan that folder with a script and find the links. Attach a trigger to it with a, for example, 5 minute interval and write those links on a spreadsheet.

an example:

function links(){
var sheet = SpreadsheetApp.getActive.getActiveSpreadsheet();
var folder = DriveApp.getFolderById('put google folder id here');
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var vals = file.getUrl();
sheet.getRange(your range you want to paste the link).setValues(vals);
}
}

This will find the links of all the files in the folder and paste that onto a google spreadsheet where the code is linked to.
Use that spreadsheet as a database and send an email by using that database.

Create direct links from URLs to files on Google Drive in Google Sheets

Put these scripts into your script editor. Run the onOpen once it will create a menu with one item. Now go back to your spreadsheet and make sure that there is a blank column to the right of your list of filenames and then select all of your filenames and the cells to the right of them. And then from the My Tools menu select "Get Links to Files". It will put the file id's next to all of the selected files. It's been running slowly today so be patient. Also if one of the files causes an error I have no error recovery so you'll have to do that one by hand. I wouldn't select too many at one time because there is a 6 minute script timeout.

function onOpen()
{
SpreadsheetApp.getUi().createMenu('My Tools')
.addItem('Get Links to Files','testFind')
.addToUi();
}

function testFind()
{
var sht = SpreadsheetApp.getActiveSheet();
var rng = sht.getActiveRange();
var rngA = rng.getValues();
for(var i=0;i<rngA.length;i++)
{
rngA[i][1]=findFileByNameReturnId(rngA[i][0]);
}
rng.setValues(rngA);
}

function findFileByNameReturnId(name)
{
var rv = '';
var files = DriveApp.getFiles();
while (files.hasNext())
{
var file = files.next();
if(file.getName()==name)
{
rv = file.getId();
}
}

Get a shareable link of a file in our google drive using Colab notebook

You can use xattr to get file_id

from subprocess import getoutput
from IPython.display import HTML
from google.colab import drive
drive.mount('/content/drive') # access drive
# need to install xattr
!apt-get install xattr > /dev/null
# get the id
fid = getoutput("xattr -p 'user.drive.id' '/content/drive/My Drive/Colab Notebooks/R.ipynb' ")
# make a link and display it
HTML(f"<a href=https://colab.research.google.com/drive/{fid} target=_blank>notebook</a>")

Here I access my notebook file at /Colab Notebooks/R.ipynb and make a link to open it in Colab.

Google App Script: how to display google drive files with links to templated HTML

Here is a Sample Code:

Note:

  • I temporarily removed <?!= include('Stylesheet'); ?> in the html file since it is not defined.

Code.gs

function doGet() {
var output = HtmlService.createTemplateFromFile('Page').evaluate();
return output;
}

function include(filename){
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}

function displayData() {
var dir = 'Somefoldername';
var foldername = DriveApp.getFoldersByName(dir).next();
var foldercont = foldername.getFiles();
var listicon = '<img src="https://drive-thirdparty.googleusercontent.com/16/type/application/vnd.google-apps.document">';
var file, title, links, list = [];
while (foldercont.hasNext()) {
file = foldercont.next();
title = file.getName();
links = file.getUrl();
date = file.getDateCreated();

list.push('<tr><td>' + listicon + '<a href ="' + links + '">' + title +'</td></tr>');
}
return list.join(' ');
}

Modifications done:

  • Replace while (contents.hasNext()) with while (foldercont.hasNext())
  • Combine your array list into a single string using array.join(' ') with spaces as its separator

Page.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
google.script.run.withSuccessHandler(function(tblStr){
document.getElementById('FileList').innerHTML = tblStr;
}).displayData();
</script>
<table id="FileList">
</table>
</body>
</html>

Modifications done:

  • I added a table in the html body with the id "FileList"
  • I called the server-side function displayData() using google.script.run.withSuccessHandler(function).displayData(). The return value of displayData() will be passed to the callback function's first parameter.
  • I updated the table's content based on the displayData()'s return value using this procedure document.getElementById('FileList').innerHTML = tblStr;

Output:

Sample Image



Related Topics



Leave a reply



Submit