Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets 如何检查图像公式中使用的url是否正确以及图像是否显示?_Google Sheets_Google Sheets Formula - Fatal编程技术网

Google sheets 如何检查图像公式中使用的url是否正确以及图像是否显示?

Google sheets 如何检查图像公式中使用的url是否正确以及图像是否显示?,google-sheets,google-sheets-formula,Google Sheets,Google Sheets Formula,我有一个使用文件名引用的图像列表: =image("domainname.com/somepath/"&A2&".jpg") 其中A2是一个文件名 不幸的是,其中一些图像具有.png扩展名。 我试图找到一个解决方案来检查扩展名为jpg的文件是否正确,是否找到了图像。如果找不到,我想用 =image("domainname.com/somepath/"&a2&".png") 这个牢

我有一个使用文件名引用的图像列表:

=image("domainname.com/somepath/"&A2&".jpg")
其中A2是一个文件名

不幸的是,其中一些图像具有.png扩展名。 我试图找到一个解决方案来检查扩展名为jpg的文件是否正确,是否找到了图像。如果找不到,我想用

 =image("domainname.com/somepath/"&a2&".png")
这个牢房

IFNA和IFERRO公式不起作用。 我还尝试连接公式:

=image("domainname.com/somepath/"&a2&".png")&image("domainname.com/somepath/"&a2&".jpg")
但是我知道你可以使用&仅用于字符串

在不同的文件夹中有数千张图片供参考,我无法控制它们的格式。
你有什么想法吗?

我建议的一种解决方法是创建并使用自定义函数来验证图像URL

  • GoogleSheets提供了数百个内置函数,如AVERAGE、SUM和VLOOKUP。当这些还不足以满足您的需求时,您可以使用谷歌应用程序脚本编写自定义函数——比如,将米转换为英里或从互联网获取实时内容——然后像内置函数一样在谷歌工作表中使用它们
示例自定义函数:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
  • 此自定义函数将接受1个图像URL参数,并使用try-catch方法验证该URL是否有效。当URL有效时,函数将返回1;如果URL无效,函数将返回0
请注意,@customfunction标记在函数注释中很重要,因此当您在单元格中键入时,此自定义函数将显示为建议的公式


创建自定义函数后,可以使用该函数验证URL,并使用IF函数使用正确的图像URL

示例:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
样本输出:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))



(更新)

优化解决方案:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
示例自定义函数:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
  • 此解决方案接受不带文件扩展名的图像URL,并返回具有有效文件扩展名的图像URL
表格公式:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))

我建议的一种解决方法是创建并使用自定义函数来验证图像URL

  • GoogleSheets提供了数百个内置函数,如AVERAGE、SUM和VLOOKUP。当这些还不足以满足您的需求时,您可以使用谷歌应用程序脚本编写自定义函数——比如,将米转换为英里或从互联网获取实时内容——然后像内置函数一样在谷歌工作表中使用它们
示例自定义函数:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
  • 此自定义函数将接受1个图像URL参数,并使用try-catch方法验证该URL是否有效。当URL有效时,函数将返回1;如果URL无效,函数将返回0
请注意,@customfunction标记在函数注释中很重要,因此当您在单元格中键入时,此自定义函数将显示为建议的公式


创建自定义函数后,可以使用该函数验证URL,并使用IF函数使用正确的图像URL

示例:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
样本输出:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))



(更新)

优化解决方案:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
示例自定义函数:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))
  • 此解决方案接受不带文件扩展名的图像URL,并返回具有有效文件扩展名的图像URL
表格公式:

/**
 * validate image
 *
 * @param {input} image url to validate.
 *
 * @customfunction
 */
function IMAGE_TRY(input) {
  
  try{
      var response = UrlFetchApp.fetch(input);
      return 1;
  } catch(err) {
    
      return 0;
  }

}
=IF(IMAGE_TRY("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"),image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.jpg"), image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp.png"))
/**
 * get image url with correct file extension.
 *
 * @param {input} image url string without extension.
 *
 * @customfunction
 */
function getURL(input) {
  
  try{
      var response = UrlFetchApp.fetch(input+".jpg");
       return (input.concat(".jpg"));
  } catch(err) {
    
      return (input.concat(".png"));
  }

}
=image(getURL("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_160x56dp"))

你好谢谢它起作用了!不客气。我很高兴能为您提供帮助,如果它有任何附加价值,这里有一篇文章将带您了解上述相同的逻辑,也许更详细,但它与Ron已经提供的答案相同。回答得好!欢迎光临@KrzysztofDołęgowski,我添加了一个优化的解决方案,可以减少获得正确图像所需的公式。请随意选择您喜欢的解决方案。您好!谢谢它起作用了!不客气。我很高兴能为您提供帮助,如果它有任何附加价值,这里有一篇文章将带您了解上述相同的逻辑,也许更详细,但它与Ron已经提供的答案相同。回答得好!欢迎光临@KrzysztofDołęgowski,我添加了一个优化的解决方案,可以减少获得正确图像所需的公式。请随意选择您喜欢的解决方案。