SOLVED

Autofill YouTube data

%3CLINGO-SUB%20id%3D%22lingo-sub-1157726%22%20slang%3D%22en-US%22%3EAutofill%20YouTube%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1157726%22%20slang%3D%22en-US%22%3E%3CP%3EI%20would%20like%20to%20create%20a%20spreadsheet%20where%20I%20paste%20a%20%3CEM%3EYouTube%3C%2FEM%3E%20link%20into%20a%20cell%20in%20%3CEM%3EColumn%20A%3C%2FEM%3E%2C%20and%20then%20%3CEM%3EColumn%20B%3C%2FEM%3E%20and%20%3CEM%3EC%3C%2FEM%3E%20autofill%20with%20the%20video's%20%3CEM%3Etitle%3C%2FEM%3E%20and%20%3CEM%3Eduration%3C%2FEM%3E%2C%20respectively.%26nbsp%3B%20Is%20this%20possible%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1157726%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1158048%22%20slang%3D%22en-US%22%3ERe%3A%20Autofill%20YouTube%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1158048%22%20slang%3D%22en-US%22%3EHello%2Cyou%20have%20to%20type%20in%20the%20video%20Title%20and%20Duration%20into%20columns%20B%20and%20C..%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1161242%22%20slang%3D%22en-US%22%3ERe%3A%20Autofill%20YouTube%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1161242%22%20slang%3D%22en-US%22%3ECan%20this%20be%20done%20automatically%20with%20a%20macro%20or%20some%20code%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1161670%22%20slang%3D%22en-US%22%3ERe%3A%20Autofill%20YouTube%20data%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1161670%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F548390%22%20target%3D%22_blank%22%3E%40DataCruncher%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYes%2C%20that%20could%20be%20done.%20You%20need%20to%20pickup%20metadata%20from%20the%20page%20and%20extract%20proper%20content.%20In%20Google%20Sheets%20exists%20the%20function%20IMPORTXML()%20which%20can%20do%20that%20directly.%20There%20is%20no%20such%20function%20in%20Excel%2C%20but%20you%20could%20find%20VBA%20equivalents%20in%20internet.%20Combine%20one%20with%20another%20it's%20possible%20to%20build%20VBA%20solution.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPerhaps%20another%20variant%20in%20Excel%20is%20combination%20of%20WEBSERVICE()%20and%20FILTERXML()%2C%20nut%20sure%20-%20didn't%20play%20with%20them.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20more%20variant%20is%20Power%20Query%20-%20to%20query%20table%20with%20URL%3As%2C%20load%20web%20content%20as%20table%20of%20texts%2C%20parse%20it%20and%20extract%20required%20metadata.%3C%2FP%3E%0A%3CP%3EAs%20a%20sample%2C%20let%20use%20two%20functions%3C%2FP%3E%0A%3CP%3Eto%20extract%20Title%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E(URL%20as%20text)%20%3D%26gt%3B%0Alet%0A%20%20%20%20%2F%2FURL%20%3D%20%22https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxy9nmSQeUWg%22%2C%0A%20%20%20%20GetPage%20%3D%20Web.Contents(URL)%2C%0A%20%20%20%20PageAsText%20%3D%20Table.FromColumns(%7BLines.FromBinary(GetPage%2Cnull%2Cnull%2C65001)%7D)%2C%0A%20%20%20%20FiltereMetaName%20%3D%20Table.SelectRows(%0A%20%20%20%20%20%20%20%20PageAsText%2C%0A%20%20%20%20%20%20%20%20%20each%20Text.Contains(%5BColumn1%5D%2C%20%22%26lt%3Bmeta%20name%3D%22%22%20title%3D%22%22%20%2F%26gt%3B%22%2C%22%22%2C%0A%20%20%20%20%20%20%20%20Replacer.ReplaceText%2C%7B%22Title%22%7D%0A%20%20%20%20)%7B0%7D%5BTitle%5D%0Ain%0A%20%20%20%20GetTitle%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Eto%20extract%20Duration%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E(URL%20as%20text)%20%3D%26gt%3B%0A%0Alet%0A%20%20%20%20%2F%2FURL%20%3D%20%22https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxy9nmSQeUWg%22%2C%0A%20%20%20%20%0A%20%20%20%20GetPage%20%3D%20Web.Contents(URL)%2C%0A%20%20%20%20PageAsText%20%3D%20Table.FromColumns(%7BLines.FromBinary(GetPage%2Cnull%2Cnull%2C65001)%7D)%2C%0A%20%20%20%20FiltereMetaName%20%3D%20Table.SelectRows(%0A%20%20%20%20%20%20%20%20PageAsText%2C%0A%20%20%20%20%20%20%20%20%20each%20Text.Contains(%5BColumn1%5D%2C%20%22%26lt%3Bmeta%20itemprop%3D%22%22%20duration%3D%22%22%20%2F%26gt%3B%22%2C%22%22%2C%0A%20%20%20%20%20%20%20%20Replacer.ReplaceText%2C%7B%22Duration%22%7D%0A%20%20%20%20)%2C%0A%20%20%20%20Duration%20%3D%20Time.FromText(%0A%20%20%20%20%20%20%20%20Text.PadStart(CleanDuration%7B0%7D%5BDuration%5D%2C6%2C%220%22)%0A%20%20%20%20)%0Ain%0A%20%20%20%20Duration%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWith%20them%20main%20query%20could%20be%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22tblVideos%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20AddTitle%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20Source%2C%0A%20%20%20%20%20%20%20%20%22Title%22%2C%0A%20%20%20%20%20%20%20%20each%20fnGetVideoTitle(%5BURL%5D)%0A%20%20%20%20)%2C%0A%20%20%20%20AddDuration%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20AddTitle%2C%0A%20%20%20%20%20%20%20%20%22Duration%22%2C%0A%20%20%20%20%20%20%20%20each%20fnGetVideoDuration(%5BURL%5D)%0A%20%20%20%20)%0Ain%0A%20%20%20%20AddDuration%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EResult%20is%20like%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20863px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F169850i29F73AFF27889F34%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EMinus%20is%20that's%20separate%20table%20with%20not%20clickable%20URL%20and%20you%20shall%20not%20to%20forget%20refresh%20it%20with%20adding%20new%20data.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

I would like to create a spreadsheet where I paste a YouTube link into a cell in Column A, and then Column B and C autofill with the video's title and duration, respectively.  Is this possible?

4 Replies
Hello,you have to type in the video Title and Duration into columns B and C..
Can this be done automatically with a macro or some code?
best response confirmed by DataCruncher (New Contributor)
Solution

@DataCruncher 

Yes, that could be done. You need to pickup metadata from the page and extract proper content. In Google Sheets exists the function IMPORTXML() which can do that directly. There is no such function in Excel, but you could find VBA equivalents in internet. Combine one with another it's possible to build VBA solution.

 

Perhaps another variant in Excel is combination of WEBSERVICE() and FILTERXML(), nut sure - didn't play with them.

 

One more variant is Power Query - to query table with URL:s, load web content as table of texts, parse it and extract required metadata.

As a sample, let use two functions

to extract Title

(URL as text) =>
let
    //URL = "https://www.youtube.com/watch?v=xy9nmSQeUWg",
    GetPage = Web.Contents(URL),
    PageAsText = Table.FromColumns({Lines.FromBinary(GetPage,null,null,65001)}),
    FiltereMetaName = Table.SelectRows(
        PageAsText,
         each Text.Contains([Column1], "<meta name=""title"" content=")
    ),
    SplitByMetaName = Table.SplitColumn(
        FiltereMetaName,
        "Column1",
        Splitter.SplitTextByDelimiter("<meta name=""title"" content=""", QuoteStyle.None),
        {"Column1.1", "Title"}
    ),
    RemoveUnused = Table.RemoveColumns(
        SplitByMetaName,
        {"Column1.1"}
    ),
    GetTitle = Table.ReplaceValue(
        RemoveUnused,
        """>","",
        Replacer.ReplaceText,{"Title"}
    ){0}[Title]
in
    GetTitle

to extract Duration

(URL as text) =>

let
    //URL = "https://www.youtube.com/watch?v=xy9nmSQeUWg",
    
    GetPage = Web.Contents(URL),
    PageAsText = Table.FromColumns({Lines.FromBinary(GetPage,null,null,65001)}),
    FiltereMetaName = Table.SelectRows(
        PageAsText,
         each Text.Contains([Column1], "<meta itemprop=""duration"" content=")
    ),
    TrimmIt = Table.TransformColumns(
        FiltereMetaName,
        {{"Column1", Text.Trim, type text}}
    ),
    SplitbyMeta = Table.SplitColumn(
        TrimmIt,
        "Column1",
        Splitter.SplitTextByDelimiter("<meta itemprop=""duration"" content=""PT", QuoteStyle.None),
        {"Column1.1", "Duration"}
    ),
    RemoveUnused = Table.RemoveColumns(
        SplitbyMeta,
        {"Column1.1"}
    ),
    ReplaceHr = Table.ReplaceValue(
        RemoveUnused,
        "H","",
        Replacer.ReplaceText,{"Duration"}
    ),
    ReplaceMin = Table.ReplaceValue(
        ReplaceHr,
        "M","",
        Replacer.ReplaceText,{"Duration"}
    ),
    CleanDuration = Table.ReplaceValue(
        ReplaceMin,
        "S"">","",
        Replacer.ReplaceText,{"Duration"}
    ),
    Duration = Time.FromText(
        Text.PadStart(CleanDuration{0}[Duration],6,"0")
    )
in
    Duration

With them main query could be

let
    Source = Excel.CurrentWorkbook(){[Name="tblVideos"]}[Content],
    AddTitle = Table.AddColumn(
        Source,
        "Title",
        each fnGetVideoTitle([URL])
    ),
    AddDuration = Table.AddColumn(
        AddTitle,
        "Duration",
        each fnGetVideoDuration([URL])
    )
in
    AddDuration

Result is like

image.png

Minus is that's separate table with not clickable URL and you shall not to forget refresh it with adding new data.

awesome, thanks!